Home Blog Tools Contact

How to Count Characters in JavaScript?


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.

1
2
var str = "Hello, world!";
console.log(str.length); // 13

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.

1
2
var str = " Hello, world! ";
console.log(str.trim().length); // 12

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.

1
2
3
4
5
6
7
8
9
var str = "Hello, world!";

var regex = /[a-zA-Z0-9]/g; // only count letters and numbers
console.log(str.match(regex).length); // 10

var str = "Hello, world!";

var regex = /[^a-z]/gi; // only count letters
console.log(str.replace(/[^a-z]/gi, "").length); // 10

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.

1
2
3
4
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.

1
2
3
4
5
6
7
var str = "Hello world!";
var count = 0;

Array.prototype.forEach.call(str, function(c) {
  count++;
});
console.log(count); // 13

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!";

1
2
3
4
5
var count = 0;
for (var i = 0; i < str.length; i++) {
  count++;
}
console.log(count); // 13

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.

1
2
3
4
5
6
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.

1
2
3
4
5
6
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.