Or press ESC to close.

How to Count Characters using JavaScript

24 Jul 2022 4 Min read
javascript logo

JavaScript is a versatile scripting language that can be used for a variety of purposes. One such purpose is to count the number of characters in a given string of text. This can be useful for a number of reasons, such as determining the length of a text field input or displaying a character count to users.

Count characters in JavaScript using length property

There are a few different ways to count characters in JavaScript. The most basic way is to use the .length property, which is available on all strings. This will return the number of characters in the string, including whitespace and other non-visible characters.

var str = "Hello, world!";

console.log(str.length); // prints 13 to the console

Count non-whitespace characters in JavaScript using trim property

If you only want to count the non-whitespace characters in a string, you can use the .trim() method before checking the .length property. This will remove any leading or trailing whitespace from the string, which will result in a more accurate character count.

var str = " Hello, world! ";

console.log(str.trim().length); // printes 12 to the console

Count characters in JavaScript using regular expression (regex)

Another way to count characters in JavaScript is to use a regular expression. This approach can be more versatile, as you can specify exactly what should be counted as a character. For example, you could use a regular expression to only count letters and numbers, or to exclude certain characters from the count.

var str = "Hello, world 123!";

var regex = /[a-zA-Z0-9]/g; // only count letters and numbers

console.log(str.match(regex).length); // prints 13 to the console


var str = "Hello, world 123!";

var regex = /[^a-z]/gi; // only count letters

console.log(str.replace(/[^a-z]/gi, "").length); // prints 10 to the console

This last one replaces all non-letter characters with an empty string, then counts the resulting string. Note that this will not work for non-Latin alphabets.

Split string into array using regex

You can also use a regular expression to split a string into an array of characters, which can then be counted using the .length property.

var str = "Hello, world!";

var regex = /\S/g; // split on non-whitespace characters

console.log(str.split(regex).length - 1); // 12 (11 characters + 1 for the empty string at the end)

Count characters in JavaScript using forEach

The Array.prototype.forEach method provides another way of counting characters in JavaScript.

var str = "Hello, world!";

var count = 0;

Array.prototype.forEach.call(str, function(c) {

    count++;

});

console.log(count); // prints 13 to the console

This iterates over each character in the string and increments a counter. Note that this method is not supported in IE8 and earlier versions.

Count characters in JavaScript using for loop

If you need to support older browsers, you can use a for loop:

var str = "Hello, world!";

var count = 0;

for (var i = 0; i < str.length; i++) {

    count++;

}

console.log(count); // prints 13 to the console

This approach can be slightly more tedious, but it can also be more accurate as you have more control over what counts as a character.

In addition, this method can also be used to count characters in any type of data, not just strings.

var data = [1,2,3,4,5];

var count = 0;

for (var i = 0; i < data.length; i++) {

    count++;

}

console.log(count); // prints 5 to the console

You can also use this method to count the number of properties in an object.

var data = {a: 1, b: 2, c: 3};

var count = 0;

for (var key in data) {

    count++;

}

console.log(count); // prints 3 to the console

There are other ways to count characters in JavaScript, but these are the most common methods.