Word Counter in Textarea using JavaScript Last Updated : 31 Oct, 2025 Comments Improve Suggest changes 91 Likes Like Report Counting words may be useful in scenarios where the user is recommended to enter a certain number of words and the word counter can keep track of the same. Below are approaches to make a word count in textarea using JavaScript:[Approach 1]: Calculating the number of spaces present in the textThis method relies on the number of spaces present in the input string to count the number of words as every word in a sentence is separated by a space.The function countWord() is defined which takes the text present in the textarea and counts the number of spaces present in it. The input text in the text area is selected by using the getElementById() method.The drawback of this method is that multiple spaces between words would be counted as multiple words, hence it may cause the word count to be unreliable. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body style="text-align: center;"> <h1 style="color: green"> GeeksforGeeks </h1> <p> Type in the textbox and click on the button to count the words </p> <textarea id="inputField" rows=10 cols="60"> </textarea> <br><br> <button onclick="countWords()"> Count Words </button> <br><br> <p> Word Count: <span id="show">0</span> </p> <script> function countWords() { // Get the input text value let text = document .getElementById("inputField").value; // Initialize the word counter let numWords = 0; // Loop through the text // and count spaces in it for (let i = 0; i < text.length; i++) { let currentCharacter = text[i]; // Check if the character is a space if (currentCharacter == " ") { numWords += 1; } } // Add 1 to make the count equal to // the number of words // (count of words = count of spaces + 1) numWords += 1; // Display it as output document.getElementById("show") .innerHTML = numWords; } </script> </body> </html> [Approach 2]: Separating the words based on spaces and then counting the number of wordsWe improve the drawback of the previous approach by splitting the words on the basis of the space character and then checking that each split is not only a space character. The countWord() function is called every time the user types in something into the text area using the oninput event handler on the text area. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body style="text-align: center;"> <h1 style="color: green"> GeeksforGeeks </h1> <p> Type in the textbox to automatically count the words </p> <textarea id="word" oninput="countWord()" rows="10" cols="60"> </textarea> <br><br> <p> Word Count: <span id="show">0</span> </p> <script> function countWord() { // Get the input text value let words = document .getElementById("word").value; // Initialize the word counter let count = 0; // Split the words on each // space character let split = words.split(' '); // Loop through the words and // increase the counter when // each split word is not empty for (let i = 0; i < split.length; i++) { if (split[i] != "") { count += 1; } } // Display it as output document.getElementById("show") .innerHTML = count; } </script> </body> </html> [Approach 3]: Count the words starting from the new lineSince the above two approaches were only able to count words when written in continuation giving spaces, but it was not able to count numbers when each word starts with a new line. So this approach will be able to count the words starting from the new line. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body style="text-align: center"> <h1 style="color: green">GeeksforGeeks</h1> <p> Type in the textbox to automatically count the words </p> <textarea id="word" rows="10" cols="60"> </textarea> <br /><br /> <p> Word Count: <span id="show">0</span> </p> <script> document .querySelector("#word") .addEventListener("input", function countWord() { let res = []; let str = this.value .replace(/[\t\n\r\.\?\!]/gm, " ").split(" "); str.map((s) => { let trimStr = s.trim(); if (trimStr.length > 0) { res.push(trimStr); } }); document.querySelector("#show") .innerText = res.length; }); </script> </body> </html> Create Quiz Comment R rohit2sahu Follow 91 Improve R rohit2sahu Follow 91 Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2020 JavaScript-Questions +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like