How to Count Number of Word Present in String.


In this post you’ll learn how to calculate words present in a string using JavaScript.

count the number of words present in the string

Example 1: With Split, Trim, and Replace Method

Steps to get number of words in a string.

Step 1: Remove side spaces from the string.

First we’ll take the string and remove the spaces from the sides by trim method.

Step 2: Replace multiple spaces between words by single space.

Then we’ll replace multiple spaces between words by a single space by replacing the method with a regular expression.

Step 3: Converting string to array by space between words.

After that we’ll split the string by space by a split method making a string and array.

Step 4: Calculating length of array.

After converting string into an array we’ll simply find its length by the .length method. And we’ll get the number of words present in the string.


    let string = "Hello   welcome  to";

    let words_count = 0;
    let array = string.trim().replace(/\s+/g, " ").split(" ");
    words_count = array.length;
    // If the string is empty then we'll set words_count to 0;
    if (array[0].length == 0) {
        words_count = 0;
    console.log("Words: ", words_count);


word counter

Their's also an another methods you can use to count the words present in a string.

Example 2: With For Loop.


let string = "Hello   welcome to";

    // splitting the string from space making an array.
    let array = string.trim().split(" ");
    // For storing the string after replacing the multiple space between words to single space.
    let words = "";
    let words_count = 0;

    // Replacing multiple spaces between words to single space using for loop.
    for (let i = 0; i < array.length; i++) {
        const element = array[i];
        if (element.length != 0) {
            // Adding words inside a separate variable with single space. We'll add element value only if its not empty ("").
            words += element + " ";
            // Counting the words.
            words_count = words.trim().split(" ").length;

    console.log("Words: ", words_count);

Example 3: With Filter Method.


    let string = "Hello   welcome to";
    console.log("String: ", string);

    let array = string.trim().split(" ");

    let words_count = array.filter(function (array) { return array });
    console.log("Words: ", words_count.length);

So that's how you can count the number words present in string using JavaScript Their may be more trick to count the words in a string. If you have any questions or suggestion you can write in the comment section.

You may like:

  1. How to create a simple and live word counter using JavaScript.
  2. How to create a character counter. Count characters with space and without space using JavaScript.
  3. How to replace multiple spaces in a string with a single space using JavaScript.
  4. How to remove all spaces from string using JavaScript.
  5. How to capitalize first letter of every word present in string using JavaScript.


Post a Comment

Post a Comment (0)
To Top