How to calculate the number of words in a string using jQuery

  1262 views   2 years ago jQuery

Use the JavaScript split() method

You can calculate or find the number of words in a string using the JavaScript split() method. This method simply split a string into an array of substrings by a specified character.

In the following example we have also used the trim() method to remove the leading and trailing white spaces from the string before counting the numbers of words.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>jQuery Count Number of Words in a String</title>
<script src=""></script>
            var words = $.trim($("textarea").val()).split(" ");
    <textarea cols="50">The quick brown fox jumps over the lazy dog.</textarea>
    <button type="button">Count Words</button>


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]