count characters in a string javascript code example

Example 1: js character certain count

var str = "Hello Lewes";
var ch = 'e';
 
var count = str.split("e").length - 1;
console.log(count);
 
/*
    Output: 3
*/

Example 2: javascript string lentrh

var myString = "string test";
var stringLength = myString.length;

console.log(stringLength); // Will return 11 because myString 
// 							  is 11 characters long...

Example 3: javascript length

var colors = ["Red", "Orange", "Blue", "Green"];
var colorsLength=colors.length;//4 is colors array length 

var str = "bug";
var strLength=str.length;//3 is the number of characters in bug

Example 4: how get count of letters in javascript

//when use length for string this will be return the count of charactor 
//in string
$("#about_me_textarea").val().length

Example 5: javascript check string lenght

let SomeString = "Example";


console.log(SomeString.length)

Example 6: count value a to b character javascript

let counter = str => {
  return str.split('').reduce((total, letter) => {
    total[letter] ? total[letter]++ : total[letter] = 1;
    return total;
  }, {});
};

counter("aabsssd"); // => { a: 2, b: 1, s: 3, d: 1 }

Tags:

Cpp Example