JavaScript - How to Count Words of a String?
Here are the various methods to count words of a string in JavaScript.
1. Using split() Method
The simplest and most common approach is to use the split() method to divide the string into an array of words and count the length of the array.
const count = (s) => s.trim().split(/\s+/).length;
const s = "Hello, this is a simple test.";
console.log(count(s));
Output
6
- Trim the leading and trailing spaces with trim().
- Splitting the string using a regular expression /\s+/, which matches one or more whitespace characters.
2. Using Regular Expressions
You can use a regular expression to match all words in a string and count the matches.
const count = (s) => (s.match(/\b\w+\b/g) || []).length;
const s = "Hello, this is a simple test.";
console.log(count(s));
Output
6
The regex /\b\w+\b/g matches word boundaries (\b) and considers sequences of alphanumeric characters as words.
3. Using reduce() Method
This approach splits the string into an array of words and uses reduce() to count them.
const count = (s) =>
s.trim().split(/\s+/).reduce((count) => count + 1, 0);
const s = "Hello, this is a simple test.";
console.log(count(s));
Output
6
This method is useful if you want to perform additional operations during word counting.
4. Using a Loop
You can iterate through the string manually, counting words whenever a whitespace or end of the string is encountered.
const count = (s) => {
let c = 0;
let inWord = false;
for (const char of s) {
if (/\s/.test(char)) {
inWord = false;
} else if (!inWord) {
inWord = true;
c++;
}
}
return c;
};
const s = "Hello, this is a simple test.";
console.log(count(s));
Output
6
This approach provides full control over the word counting process.
5. Using matchAll() Method
The matchAll() method offers a clean way to count words by matching them with a regular expression.
const count = (s) => [...s.matchAll(/\b\w+\b/g)].length;
const s = "Hello, this is a simple test.";
console.log(count(s));
This is a modern and concise way to count word matches using an iterator.
6. Using Array.from() and filter() Methods
By splitting the string into characters, you can use Array.from() with filtering to count words.
const count = (s) =>
Array.from(s.split(/\s+/).filter(Boolean)).length;
const s = "Hello, this is a simple test.";
console.log(count(s));
Output
6
This approach is useful if you want to remove empty strings during processing.
Which Approach Should You Use?
Approach | When to Use |
---|---|
Using split() | Best for simplicity and common use cases. |
Using Regular Expressions | Ideal for precise control over what is considered a word. |
Using reduce() | Use when combining word counting with additional operations. |
Using a Loop | Best for custom logic or low-level string processing. |
Using matchAll() | Modern and concise approach for counting regex matches. |
Using Array.from() and Filter | Use when filtering or processing split results is required. |
The split() and regular expression methods are the most commonly used and efficient.