JavaScript character count | String length

Use javascript length property to count characters in the given string. This way you will get the number of characters in a string.

Example of JavaScript character count

See below example how you can count the char in string using string.length property:-

<!DOCTYPE html>
<html>
    <head>
        <title> Example</title>
        <script type="text/javascript">
		var str = "Hello World!";
		var n = str.length;
		// show output in alert
        	alert(n)
      
        </script>
    </head>
    
</html>

Output:

JavaScript character count

TextArea character count JavaScript Example

Using jQuery you can count characters in a textarea. Like how many char can you type inside a TextArea.

<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-1.5.js"></script>
    <script>
      function countChar(val) {
        var len = val.value.length;
        if (len >= 500) {
          val.value = val.value.substring(0, 500);
        } else {
          $('#charNum').text(500 - len);
        }
      };
    </script>
  </head>

  <body>
    <textarea id="field" onkeyup="countChar(this)"></textarea>
    <p>Char Left:</p>  <p id="charNum"> </p> 
   
  </body>

</html>

Output:

TextArea character count JavaScript Example

Q: How to count occurrences of each character in string javascript?

For example given string is:-

var mainStr = "str1,str2,str3,str4";

Find the count of comma , character, which is 3. And the count of individual strings after the split along with a comma, which is 4.

Answer: Use a regular expression 

<!DOCTYPE html>
<html>
    <head>
        <title> Example</title>
        <script type="text/javascript">

		console.log(("str1,str2,str3,str4".match(/,/g) || []).length); //logs 3

		console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); //logs 4
      
        </script>
    </head>
    
</html>

Output:

count occurrences of each character in string javascript

Q: How to Count the frequency of characters in a string using javascript?

For example: “aabsssd”

Output: a:2, b:1, s:3, d:1

I also want to map the same character as a property name in an object.

Answer: Here is code of it:-

<!DOCTYPE html>
<html>
    <head>
        <title> Example</title>
        <script type="text/javascript">

		function getFrequency(string) {
    	var freq = {};
    	for (var i=0; i<string.length;i++) {
        	var character = string.charAt(i);
        	if (freq[character]) {
           		freq[character]++;
        	} else {
           		freq[character] = 1;
        	}
    	}

    		return freq;
    	};

    	var out = getFrequency("aabsssd");
    	console.log(out)
      
        </script>
    </head>
    
</html>

Output: Program output in safari console

Count the frequency of characters in a string javascript

Do comment if you have any doubts and suggestion on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.