Count Words in JavaScript: Split vs Intl.Segmenter
Blog / DEVELOPMENT

To count whitespace-separated tokens in JavaScript, trim the string, return zero for empty text, and split the remainder with /\s+/. For language-aware word segmentation, use Intl.Segmenter with word granularity and count only segments whose isWordLike property is true.
Start with a blank-safe token counter
Whitespace splitting is useful when your requirement explicitly defines a word as a nonempty piece separated by whitespace. The empty-string check prevents a blank input from being reported as one token.
The example below treats repeated spaces, tabs, and line breaks as separators. It does not examine whether each resulting token contains a linguistic word.
function countTokens(text) {
const trimmed = text.trim();
return trimmed ? trimmed.split(/\s+/u).length : 0;
}
countTokens("Map\tnew\nroutes"); // 3
countTokens(" "); // 0Use Intl.Segmenter for word boundaries
Intl.Segmenter applies locale-sensitive segmentation instead of assuming that spaces mark every word boundary. Filtering by isWordLike avoids counting the whitespace and punctuation segments returned alongside words.
Pass the appropriate locale for your content. A multilingual editor should not silently claim that a simple English-oriented whitespace rule provides a universal count.
function countWords(text, locale = "en") {
const segmenter = new Intl.Segmenter(locale, {
granularity: "word",
});
let count = 0;
for (const part of segmenter.segment(text)) {
if (part.isWordLike) count++;
}
return count;
}
countWords("Orchards bloom."); // 2Define the behavior your product needs
A token counter counts a standalone punctuation token in 'plan & build' because whitespace surrounds it. A word-boundary implementation can treat punctuation differently. Neither rule should be hidden from users who depend on the result.
Prepare test fixtures containing contractions, hyphenated phrases, numbers, emoji, and text without inter-word spaces. Record expected behavior for your application rather than assuming every editor must agree.
Check runtime support and keep counts reproducible
Verify that Intl.Segmenter exists in the browser or server runtime you support. If it does not, make any fallback explicit: silently replacing language-aware segmentation with whitespace splitting changes the definition of the result.
Keep the locale and counting approach consistent between your interface and backend. For an initial comparison, paste the same sample into the word counter, but investigate differences instead of treating a second tool as a universal standard.
const supportsSegmentation =
typeof Intl !== "undefined" &&
typeof Intl.Segmenter === "function";Frequently asked questions
Does string.length count words?
No. It measures UTF-16 code units, not words.
Why not split on a single space?
That approach creates empty pieces around repeated spaces and does not handle tabs or line breaks as separators.
Will Intl.Segmenter match every word processor?
Not necessarily. Different products can apply different counting conventions, especially around punctuation and compound terms.