I want to count the number of occurrences of each character in a given string using JavaScript.
For example:
var str = \"I want to count the number
This worked well for me :
function Char_Count(str1) { var chars = {}; str1.replace(/\S/g, function(l){chars[l] = (isNaN(chars[l]) ? 1 : chars[l] + 1);}); return chars; } var myString = "This is my String"; console.log(Char_Count(myString));