How To Build A Word Counter Using JavaScript

Home » Coding » How To Build A Word Counter Using JavaScript

Introduction of the Project

In the world of web development, JavaScript is a versatile and widely-used programming language that enables developers to build dynamic and interactive web applications. One common task that many web developers encounter is the need to count the number of words in a block of text, whether it’s for a blog post, comment section, or other types of content. Fortunately, with JavaScript, building a word counter can be a straightforward and rewarding project. In this JavaScript tutorial, we’ll explore how to build a word counter using JavaScript, from gathering user input to displaying the word count in a simple and effective manner.

Whether you’re a beginner or an experienced JavaScript developer, this guide will provide you with the tools you need to create a useful and functional word counter for your web applications. We will be implementing CSS to enhance the GUI of our application.


1. To understand the basics of HTML and CSS required to create a simple text input field and display the word count on the screen.

2. To retrieve user input from a text field and store it in an array and count each non-null string in the text using JavaScript.

3. To create an event listener to detect changes in the input field and update the word count in real time.

4. To implement a function that calculates the number of words in the user input, accounting for different edge cases and special characters.

5. To explore different ways of optimizing the word counting function for efficiency and accuracy.

6. Customize the appearance of the word count display, such as font size, color, and formatting.

7. Test the word counter on different types of input and identify potential issues or bugs.


To build a word counter using JavaScript, it’s important to have a solid understanding of the following prerequisites:

1. Visual Studio Code

2. HTML: You should be comfortable with HTML, including how to create forms and input fields.

3. CSS: You should have a basic understanding of CSS, including how to style and format HTML elements, as the word counter will need to be styled appropriately.

4. JavaScript: You should have a good grasp of JavaScript programming, including how to manipulate strings, arrays, and functions.

5. DOM Manipulation: You should be familiar with how to use JavaScript to manipulate the Document Object Model (DOM) of an HTML document to add and modify HTML elements dynamically.

6. Event handling: You should know how to add event listeners to HTML elements to detect user input and update the word count accordingly.

7. Regular expressions: You should understand how to use regular expressions to account for different edge cases and special characters in the word counting function.

8. Code optimization: You should know how to optimize your code for efficiency and accuracy.

Source Code


<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Word Counter</title>
<h1>Word Counter</h1>
<textarea name="" id="textA" cols="30" rows="10"></textarea>

<button onclick="counter()">WORDCOUNT</button>
<button onclick="reset()">RESET</button>

<div id="asd">
<label for="">WORDS:</label>
<label id="words">0</label>
<script src="word.js"></script>

CSS Code

width: 800px;;
height: 50vh;
margin-left: 25%;
text-align: center;
margin-left: 25%;
font-size: 25;
font-family: 'Courier New', Courier, monospace;
color: crimson;

margin-top: 100px;
font-size: large;
font-weight: bold;


function counter(){
var str = document.getElementById('textA').value;
var words = str.split(" ");
var count=0;
for(var i=0;i<words.length;i++){
document.getElementById('words').innerHTML = count;
function reset(){
document.getElementById('textA').value = "";
document.getElementById('words').innerHTML = 0;

Explanation of the Code

1. Our word counter consists of a text area, a label, and 2 buttons.

2. We have used CSS to style the user interface of our word counter application, where the container is provided with appropriate margins, padding, font color, and font size.

Moving to count words, we have extracted text from the text area and, counted each non-empty string & updated the counter:

1. We have created a function counter() that stores all data from the text area in an array & also removes spaces.

2. Then we created a loop through the array & increase the counter on getting a non-null string.

3. Finally, we are displaying the word count in the UI.

4. There is a reset button also provided to the user that removes all data from the text area and resets the word counter to 0.


Main Interface

Word Counter Using JavaScript

Points To Remember

We have successfully learned how to build a word counter using JavaScript. We have built a really easy user interface where users can easily get the word count by entering data in the text area. This website can be useful in essay writing where there is a word limit.

Here are some important points to remember while building a word counter using JavaScript:

  • Account for edge cases: The word counting function should account for edge cases, such as empty input, repeated spaces, and punctuation, to ensure accurate word counting.
  • Optimize the code: The code should be optimized for efficiency and accuracy using appropriate data structures and algorithms.
  • Real-time update: The word count should be updated in real-time as the user types, using an event listener to detect changes in the input field.
  • User interface: The word counter should have a clear and user-friendly interface, with clear instructions and feedback to the user.
  • Cross-browser compatibility: The word counter should be tested and work across different web browsers and platforms.
  • Documentation: It’s important to document your code, including comments and clear variable and function names, to make it easier to understand and maintain.
  • Test thoroughly: The word counter should be tested thoroughly with various inputs to ensure it works correctly and efficiently.

By keeping these points in mind, you can build a functional and effective word counter using JavaScript that will be useful for your website or application.


More JavaScript Projects>>>

You May Also Like To Create…


Submit a Comment

Your email address will not be published. Required fields are marked *