Skip to content

Word Counter

Count your words, characters, and sentences here:

Number of words: 0
Characters with spaces: 0
Number of sentences: 0
Characters without spaces: 0

The Word Counter counts the number of words and characters in your text

It is often necessary to count the words and characters in a text. That's why I built a simple and fast word counter. You just copy your text in, and it automatically counts:

When should you count words and characters?

There are many situations where you need some statistics on your text.

How I count words and characters in the text

This word counter is built in JavaScript. Here you can see how I calculate the values for the individual elements. The structure of the code looks a bit different when it's running. But here you can see the core of the calculations being made.

I have removed comments from the functions because the code is explained beforehand.

How I count the number of words in the text

It's fairly simple to count the number of words in a text. I start by replacing all irrelevant characters with nothing. So standalone periods and hyphens don't count as a word.

Then I split the text into chunks of words. This gives an array of words. If the text is entered so that the new array has empty elements, we filter them out.

We can use this output for many fun things. But here we just use the length method. It counts the number of elements in our array, which corresponds to the number of words in your text.

It looks like this:

var testString = "This is just an example.";

var words = function(text) {
     return text
       .replace(/[-'.,]/gi, '')
       .split(/[^a-zA-ZæøåÆØÅ0-9]/g)
       .filter(Boolean);
};

console.log(words(testString).length) // return 6

You can open the console in your browser and try it yourself. Just copy it in.

How I count the number of characters in the text

Counting the number of characters is almost even simpler than counting words. Because everything in principle counts as a character. And the only meaningful distinction we need to make is whether the text is counted with or without spaces.

Therefore, I have created a function that first takes your text as input. And then you can choose with a Boolean whether you want to count characters with or without spaces.

It looks like this:

var testString = "This is just an example.";

var characters = function(text, spacesBoolean) {
     if (spacesBoolean) {
       return text.split('');
     } else {
       return text.replace(/\s+/g, '').split('');
     }
};

console.log(characters(testString, true).length)  // return 28
console.log(characters(testString, false).length) // return 23

You can open the console in your browser and try it yourself. Just copy it in.

How I count the number of sentences in the text

The number of sentences in the text can also be called the number of periods. We count it by splitting the text into individual sentences. This requires identifying all the characters that end a sentence. These are the ones we use in the method .split(/\?|!|.|\n/g).

Once we have an array with all the sentences in the text, we can do as in the other examples with the number of words and characters. We measure the length of our array because it is the same as the number of sentences.

var testString = "This is just an example. An additional sentence here."; 

publicAPIs.sentences = function(text) {
     var sentenceArray = [];
     var sentences = text 
       .replace(/. ([a-z])/g, ' $1')
       .replace(/.([a-z])/g, '$1')
       .split(/\?|!|.|\n/g)
       .forEach(function(element) {
         sentenceArray.push(element.trim());
       });
     return sentenceArray.filter(Boolean);
};

console.log(); // return 2

Do you need to calculate the readability index for your text?

At https://tekstr.dk/app/, I have built a readability index calculator. You can use it completely free if you need to calculate the readability index for your text.

It is used by more than 1,000 people every month.

Try the readability index calculator here.

Do you need a custom-built calculator?

I build specialized calculators that can be used to improve your customer service, gather leads, or be used for PR work.

Contact me here, and we can discuss your needs.