Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Build a Word Counter App with JavaScript

Every writer, student, and job applicant has needed a word counter at some point. Building one yourself is a perfect beginner project: one textarea, a few lines of string logic, and live updates. It teaches you event han

Every writer, student, and job applicant has needed a word counter at some point. Building one yourself is a perfect beginner project: one textarea, a few lines of string logic, and live updates. It teaches you event handling and the difference between characters, words, and reading time.

What you are building

A textarea where the user types or pastes text. Below it, live counts for words, characters (with and without spaces), sentences, and paragraphs — plus an estimated reading time. Everything updates as the user types.

Counting words correctly

The common beginner mistake is splitting on a single space. Real text has double spaces, line breaks, and tabs. Trim first, then split on any whitespace:

const text = document.getElementById("input").value;

const words = text.trim() === "" ? 0 : text.trim().split(/\s+/).length;
const characters = text.length;
const charactersNoSpaces = text.replace(/\s/g, "").length;
const sentences = text.split(/[.!?]+/).filter((s) => s.trim() !== "").length;
const paragraphs = text.split(/\n+/).filter((p) => p.trim() !== "").length;
const readingMinutes = Math.ceil(words / 200); // average reading speed

Updating live

Listen to the input event, not a button click. The counts should move while the user types:

document.getElementById("input").addEventListener("input", () => {
  const text = document.getElementById("input").value;
  const words = text.trim() === "" ? 0 : text.trim().split(/\s+/).length;

  document.getElementById("word-count").textContent = words;
  document.getElementById("char-count").textContent = text.length;
  document.getElementById("char-no-space").textContent = text.replace(/\s/g, "").length;
  document.getElementById("sentence-count").textContent =
    text.split(/[.!?]+/).filter((s) => s.trim() !== "").length;
  document.getElementById("reading-time").textContent =
    words === 0 ? "0 min" : Math.ceil(words / 200) + " min";
});

Small details that matter

Show a message when the user passes a limit they set — for example, a 300-word limit for an essay — and colour the counter red when they go over it. Add a clear button and a copy button. These are tiny features, but they are the difference between a demo and a tool someone actually uses.

Why counting words helps writing

Limits force clarity. A 150-word answer cannot hide behind filler, which is why exams, applications, and social posts all use word limits. Counting is only the start, though — writing clearly is a skill of its own, and if you want to build it, this list of best communication skills books for beginners is a good place to start.

What to add next

Add a keyword density check, a target word count with a progress bar, or a "most used words" list. Each one reuses the same split-and-count logic you already wrote.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.