Skip to content Skip to footer

Counting Words Like a Boss in JavaScript

Hey folks! If you’ve ever found yourself in need of counting words in JavaScript, buckle up! Today, we’re diving deep into the world of word counting. Whether you’re building a blog, a word processor, or just curious, I’ve got you covered.

The Vanilla JS Way

First up, let’s talk about doing it old-school with Vanilla JS. No frameworks, no libraries, just pure JavaScript.

function countWords(text) {
  // Match any sequence of characters separated by spaces
  const words = text.match(/\w+('\w+)?/g);
  return words ? words.length : 0;

const myText = "Hello, world! This is a word count test.";
console.log(countWords(myText)); // Output: 8

This function is a quick and dirty way to get the job done. It uses a regular expression to match words, considering an apostrophe within a word (like “it’s” or “they’re”).

React: Counting Words in Components

Moving on to the React world, we’re going to create a simple component that counts words as you type.

import React, { useState } from 'react';

const WordCounter = () => {
  const [text, setText] = useState('');
  const [wordCount, setWordCount] = useState(0);

  const handleTextChange = (event) => {
    const inputText =;
    setWordCount(countWords(inputText)); // Reuse our Vanilla JS function

  return (
      <textarea onChange={handleTextChange} value={text} />
      <p>Word Count: {wordCount}</p>

export default WordCounter;

In this React component, we use useState to keep track of the text and the word count. As the user types, handleTextChange updates the state, and the word count is recalculated.

Vue.js: Reactive Word Counting

If Vue.js is more your jam, here’s how you can create a reactive word counter with the Composition API.

    <textarea v-model="text" @input="updateWordCount"></textarea>
    <p>Word Count: {{ wordCount }}</p>

import { ref, computed } from 'vue';

export default {
  setup() {
    const text = ref('');
    const wordCount = computed(() => countWords(text.value));

    const updateWordCount = () => {
      // The word count will update automatically due to the reactivity system

    return {

Vue’s reactivity system makes it a breeze to update the word count. We use a computed property that automatically recalculates whenever the text changes.

Angular: Word Counting with Pipes

Angular developers, you’re not left out. Let’s create a custom pipe to count words in Angular.

import { Pipe, PipeTransform } from '@angular/core';

  name: 'wordCount'
export class WordCountPipe implements PipeTransform {
  transform(value: string): number {
    return countWords(value);

You can use this pipe in any template like so:

<textarea [(ngModel)]="text"></textarea>
<p>Word Count: {{ text | wordCount }}</p>

This pipe takes the text input and runs it through our countWords function, making it super easy to display the word count anywhere in your template.

Svelte: Word Counting with Reactive Statements

Svelte brings reactivity to the core of your code. Here’s a Svelte example:

  let text = '';
  $: wordCount = countWords(text);

<textarea bind:value={text}></textarea>
<p>Word Count: {wordCount}</p>

Svelte’s reactive declarations with $: mean that wordCount will update automatically whenever text changes. It’s concise and elegant.

Wrapping Up the First Half

Alrighty, we’ve covered the basics of word counting in JavaScript and looked at how to implement it in some of the most popular frameworks. We’ve kept it simple, focusing on the core functionality without getting bogged down in UI considerations.

Stay tuned for the second half of this article, where we’ll explore more advanced scenarios, like handling real-time updates in a collaborative editing environment, dealing with large texts efficiently, and even dipping our toes into natural language processing for more accurate word counts.

Until then, happy coding, and remember: every word counts! 😉

Welcome back, code warriors! In the first half, we covered the basics of counting words in JavaScript across different frameworks. Now, let’s level up and tackle some more advanced scenarios that you might encounter in the wild.

Real-Time Updates in Collaborative Editing

Imagine you’re building the next big collaborative editor, like Google Docs. You’ll want to update the word count as multiple users type, delete, and edit text in real-time.

Here’s how you might handle that using WebSocket for real-time communication.

// Assuming you have a WebSocket connection set up as `socket`
socket.on('text-update', (updatedText) => {
  console.log('Word Count:', countWords(updatedText));

You’d have a server broadcasting text updates to all connected clients, and each client would recalculate the word count when they receive an update. Neat, right? Just make sure your countWords function is efficient, as it’ll be called a lot.

Handling Large Texts Efficiently

When dealing with large texts, you don’t want to recount all words every time a user types a character. That’s where debouncing comes in handy.

let timeoutId;

function handleTextChange(text) {
  timeoutId = setTimeout(() => {
    console.log('Word Count:', countWords(text));
  }, 300); // Wait for 300 ms of inactivity before counting

With debouncing, you wait for a pause in typing before running the heavy word count function. This way, you’re not counting words more often than necessary.

Natural Language Processing (NLP) for Accurate Counts

For even more accurate word counts, especially in texts with complex punctuation and international characters, you might want to use Natural Language Processing (NLP). Libraries like compromise can help you parse natural language better.

const nlp = require('compromise');

function countWordsNLP(text) {
  const doc = nlp(text);
  return doc.words().length;

console.log('NLP Word Count:', countWordsNLP(myText));

compromise is a lightweight NLP library that can handle a lot of the nuances of natural language, giving you a more accurate word count for complex texts.

Serverless Functions for On-Demand Word Counting

If you’re building a static site and still want to provide word counting as a service, consider using serverless functions.

// This would be your serverless function
module.exports = async (req, res) => {
  const text = req.body.text;
  const wordCount = countWords(text);
  res.status(200).send({ wordCount });

You can deploy this function to platforms like Vercel or Netlify and call it from your frontend whenever you need a word count. This offloads the processing from the client side, which can be a big win for mobile users or those with slower devices.

Optimizing with Web Workers

For truly heavy lifting without janking the main thread, Web Workers are your friends. They let you run JavaScript in the background.

// main.js
const worker = new Worker('wordCountWorker.js');


worker.onmessage = (event) => {
  console.log('Word Count:',;

// wordCountWorker.js
self.onmessage = (event) => {
  const wordCount = countWords(;

With this setup, the main thread sends the text to be counted to the worker, which does the counting and sends back the result. Smooth as butter!


We’ve gone from the basics to the bleeding edge of word counting in JavaScript. From handling real-time updates in collaborative environments, optimizing for large texts with debouncing, leveraging NLP for accuracy, utilizing serverless functions, to offloading to Web Workers for non-blocking UIs, we’ve covered a lot of ground.

Word counting might seem simple at first glance, but as you’ve seen, there’s a lot to consider when you want to do it well. Whether you’re building the next great writing tool or just adding a word count feature to a text field, the techniques we’ve discussed will help you create a better experience for your users.

Now go forth and count words like the coding boss you are! And always remember to test, optimize, and consider the user experience in everything you build. Happy coding, and may your word counts always be accurate! 🚀