JavaScript Word Counter: Build One With Live Code

Build a JavaScript word counter that works in every language: page setup, three counting methods tested, emoji-safe characters, reading time and a live demo.

Share this post
7 min read

Last updated December 3, 2026

A JavaScript word counter reads text from a box and updates the counts as you type. The key part is how you split the text into words.

Most tutorials split on spaces. That breaks for languages like Chinese and miscounts emoji. This guide uses the browser’s built-in Intl.Segmenter instead, with a live demo below.

Key Takeaways

  • Listen for the input event on a textarea to update counts live.
  • Intl.Segmenter counts words correctly in languages without spaces.
  • Count characters by grapheme, so one emoji counts as one character.
  • Use 238 words a minute for reading time, based on a 2019 research review.
  • Different tools count hyphenated words differently, so small gaps are normal.

Live demo: Digieh word counter

Digieh tool. Words are counted with the browser’s built-in word segmenter where available. Reading time uses 238 words a minute, and reading aloud 183, from a 2019 research review. Your text never leaves your device.

Your text stays private. The demo runs only in your browser. Nothing you type is saved or sent to Digieh.

Step 1: How Do You Set Up the Page?

You need a text box and a few places to show the results.

<label for="text">Your text</label>
<textarea id="text" rows="8"></textarea>
<p>Words: <span id="words">0</span></p>
<p>Characters: <span id="chars">0</span></p>
<p>Reading time: <span id="time">0 min</span></p>

A textarea lets people paste long text. The span tags hold the numbers that JavaScript will update.

Step 2: Which Way Should You Count Words?

There are three common methods. Here is the code for each.

// Method 1: split on spaces (simple, but has a bug)
function countWordsSplit(text) {
  return text.split(' ').length;   // "" gives 1, double spaces add extra words
}

// Method 2: match runs of non-space characters
function countWordsMatch(text) {
  var m = text.match(/\S+/g);
  return m ? m.length : 0;
}

// Method 3: Intl.Segmenter, which understands each language
function countWordsSegmenter(text) {
  var seg = new Intl.Segmenter(undefined, { granularity: 'word' });
  var n = 0;
  for (var s of seg.segment(text)) {
    if (s.isWordLike) n++;
  }
  return n;
}

Digieh tested all three in Node.js 22 on December 3, 2026. The results show why the method matters.

Test textSplit on spaceMatch non-spaceIntl.Segmenter
Empty box100
Hello, world! It’s a well-known fact.667
Seven Chinese characters for I like learning to code115

Source: Digieh test in Node.js 22 using Intl.Segmenter, December 3, 2026.

The segmenter splits well-known into two words, while the other methods keep it as one. Word processors also differ on this, so small gaps between tools are normal.

5 vs 1In Digieh’s test on December 3, 2026, the segmenter found 5 words in a Chinese sentence. Space-based methods found only 1.

What Is Intl.Segmenter?

Intl.Segmenter is part of JavaScript’s built-in language tools. It splits text into graphemes, words or sentences, following the rules of each language.

Its segment method returns pieces of text, and each word piece is marked as word-like. MDN lists the method as widely available across browsers since April 2024.

Source: MDN Web Docs, Intl.Segmenter segment method, read December 3, 2026.

Older browsers may not support it. Our demo falls back to the match method when the segmenter is missing.

Step 3: How Do You Count Characters Correctly?

The length property counts code units, not the characters people see. An emoji with a skin tone can count as 4 units on its own.

// Characters: count what people see, not code units
function countChars(text) {
  var seg = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
  var n = 0;
  for (var s of seg.segment(text)) n++;
  return n;
}

In Digieh’s test, a thumbs-up emoji with a skin tone plus two letters had a length of 6. The grapheme count was 3, which matches what a reader sees.

Try this today. Paste a sentence with an emoji into the demo above. Then check the character count against what you see on screen.

Step 4: How Do You Update the Counts Live?

Listen for the input event. It fires on every key press, paste and cut.

var box = document.getElementById('text');
box.addEventListener('input', function () {
  var text = box.value;
  var words = countWordsSegmenter(text);
  document.getElementById('words').textContent = words;
  document.getElementById('chars').textContent = countChars(text);
  var minutes = Math.ceil(words / 238);   // 238 words a minute
  document.getElementById('time').textContent = minutes + ' min';
});

Math.ceil rounds up, so even a short text shows at least 1 minute. Our demo shows seconds for short texts instead.

Free updates

Join the Digieh list

Be the first to know when our weekly brief launches.

No spam. Unsubscribe anytime. Read our Privacy Policy.

How Do You Estimate Reading Time?

A 2019 review looked at 190 studies with 18,573 people. It found that adults read English non-fiction silently at about 238 words a minute. Fiction was about 260 words a minute.

The same review found that reading aloud averages about 183 words a minute. Our demo shows both times.

Source: Brysbaert, Journal of Memory and Language, 2019.

Word countSilent reading at 238 a minuteReading aloud at 183 a minute
500About 2 minutesAbout 3 minutes
1,000About 4 minutesAbout 5 minutes
2,000About 8 minutesAbout 11 minutes

Source: Digieh calculation from the Brysbaert 2019 averages, rounded to whole minutes.

How Do You Count Sentences and Paragraphs?

Sentences are harder than words. A simple rule counts text that ends in a period, question mark or exclamation mark.

Paragraphs are easier. Split on blank lines and count the parts that are not empty.

For more accurate sentences, Intl.Segmenter also supports a sentence mode. Check the Intl.Segmenter reference for details.

How Do You Show a Word Limit Warning?

Many forms limit answers to a set number of words. You can warn people as they type.

var LIMIT = 250;
if (words > LIMIT) {
  document.getElementById('words').style.color = 'red';
} else {
  document.getElementById('words').style.color = '';
}

Put this check inside the input listener, right after you count the words. You can also show how many words are left.

Remember that the limit should match how the form owner counts words. If they use a different tool, tell people the count is an estimate.

How Do You Make the Counter Accessible?

  • Give the textarea a clear label, not just placeholder text.
  • Add aria-live to the results area so screen readers announce changes.
  • Use enough color contrast for the numbers.
  • Do not rely on color alone for warnings. Add text such as “over limit”.

Our demo uses a label and an aria-live area for these reasons.

What Extra Features Can You Add?

  • A word limit that turns the count red when you go over.
  • Top keywords, by counting how often each word appears.
  • A copy button and a clear button.
  • Saving text in the browser so it is not lost on refresh.
  • Speaking time for speeches and videos.

Writing video scripts? Our guide on how to write a video script pairs well with a speaking time counter.

Building more tools? Try our tested JavaScript QR code generator guide next.

Need writing help too? See our list of free Grammarly alternatives.

What Are Common Word Counter Mistakes?

  • Using split with a single space, which counts an empty box as 1 word.
  • Ignoring line breaks and tabs as word separators.
  • Counting emoji as 2 or more characters.
  • Using a reading speed of 300 words a minute, which the 2019 review found too high.
  • Running heavy code on every key press for very long texts.

For very long texts, wait a short moment after typing stops before counting. This is called debouncing.

Which counting method will you use?

  • Split
  • Match
  • Intl.Segmenter
  • Not sure

Next, build a JavaScript tip calculator with whole-cent math and a fair bill split.

Test Yourself

What does “”.split(” “).length return in JavaScript?

  1. 0
  2. 1
  3. An error

Splitting an empty string returns an array with one empty string, so the length is 1.

What average silent reading speed did the 2019 review find for English non-fiction?

  1. 183 words a minute
  2. 238 words a minute
  3. 300 words a minute

The review found 238 words a minute for non-fiction. 183 was for reading aloud.

Frequently Asked Questions

How do I count words in JavaScript?

Use Intl.Segmenter with word granularity and count pieces marked as word-like. For a simple fallback, use text.match with a non-space pattern.

Why does my count differ from Microsoft Word?

Tools treat hyphens, numbers and symbols differently. Small differences are normal.

How do I count characters with emoji?

Use Intl.Segmenter with grapheme granularity. The length property counts code units, not visible characters.

Does Intl.Segmenter work in all browsers?

MDN lists it as widely available since April 2024. Keep a fallback for older browsers.

Conclusion

Use a textarea, the input event and Intl.Segmenter for words and characters. Add reading time at 238 words a minute, and you have a counter that works in any language.

Editor’s Note

This guide is accurate as of December 3, 2026. Code was tested in Node.js 22. Browser support comes from MDN, and reading speeds from the 2019 review linked above.

Spot an error in this article? Please contact us at support@digieh.com or read our Corrections Policy.

Keep exploring

Your next read is waiting

Found this useful?

Share this post
Avatar photo

Syed Wasif Raza

Syed Wasif Raza is the founder and editor of Digieh. He writes about health, food, lifestyle, news and sports in plain, easy English. Every guide he publishes is checked against official sources such as government and health agency pages. Connect with him on LinkedIn.

Articles: 999

Leave a Reply

Your email address will not be published. Required fields are marked *