count occurrences of character in string javascript

JavaScript
var temp = "This is a string.";
var count = (temp.match(/is/g) || []).length;
console.log(count);

Output: 2

Explaination : The g in the regular expression (short for global) says to search the whole string rather than just find the first occurrence. This matches 'is' twice.function count (string) {  
  var count = {};
  string.split('').forEach(function(s) {
     count[s] ? count[s]++ : count[s] = 1;
  });
  return count;
}function charCount(myChar, str) {
	let count = 0;

	for (let i = 0; i < str.length; i++) 
    if (str.charAt(i) == myChar) 
        count++
 return count;
} const recorrences = ['a', 'b', 'c', 'a', 'b','a']
                .map(i => !!~i.indexOf('a'))
                .filter(i => i)
                .length;
console.log(`recorrences ${recorrences}`) 
//recorrences 3
Source

Also in JavaScript: