how do i count the number of occurrences in a string javascript

JavaScript
function charCount(myChar, str) {
	let count = 0;

	for (let i = 0; i < str.length; i++) 
    if (str.charAt(i) == myChar) 
        count++
 return count;
}
Source

Also in JavaScript: