Writing content that ranks well on search engines is a lot like seasoning a soup. If you do not add enough salt, it tastes like warm water. If you dump the entire salt shaker into the pot, you ruin it. Keywords are your salt. You need them to give your text flavor so search engines know what you are cooking. But if you overdo it, you trigger spam filters and ruin the experience.
Analyzing word frequency and keyword density keeps your writing balanced. It lets you see which words show up too much, which ones need a boost, and whether your language sounds natural.
In this guide, we will break down the math behind these concepts. We will look at how search algorithms read text, write some JavaScript to build our own analyzer, and show you how to refine your copy without losing your sanity.
Why do search engines care about word frequency?
Search engine bots are not human beings. They do not read your blog post with a cup of coffee and smile at your clever metaphors. Instead, they run your text through mathematical algorithms.
When a crawler visits your page, it wants to answer one question: “What is this page actually about?”
To do that, it counts how often words appear. If you write a piece about repairing bicycles, the bot expects to see words like “bicycle,” “tire,” “chain,” “pedal,” and “wrench.” If those words show up frequently, the bot figures your page is a relevant source.
But search engines also look at the ratio of these words. If every third word is “bicycle,” the algorithm gets suspicious. It realizes you are not writing for humans. That triggers spam filters, and your rank drops. By keeping track of word frequency, you feed the bots the exact terms they expect without setting off any spam alarms.
What is keyword density and how do you calculate it?
Keyword density is the percentage of times a specific keyword or phrase appears in a document compared to the total number of words. It is a simple ratio, but it carries a lot of weight.
The mathematical formula for keyword density
Calculating keyword density is straightforward arithmetic. You take the number of times your target word appears, divide it by the total word count, and then multiply by one hundred to get a percentage.
Here is the formula:
$$\text{Keyword Density} = \left( \frac{\text{Keyword Count}}{\text{Total Word Count}} \right) \times 100$$
Let us walk through a quick example. Imagine you wrote a short blog post about coffee beans. The post is exactly 800 words long. You want to rank for the phrase “dark roast.” You search through your draft and find that you wrote the phrase “dark roast” exactly 12 times.
Plug those numbers into our formula:
$$\text{Keyword Density} = \left( \frac{12}{800} \right) \times 100 = 0.015 \times 100 = 1.5%$$
Your keyword density for “dark roast” is 1.5%. This is a safe, healthy percentage. It shows search engines what your topic is without screaming it in their faces.
What happens if you overdo it?
If you push your keyword density too high (say, up to 4% or 5%), your writing starts to sound bizarre. Here is a quick example of what keyword stuffed text looks like:
“If you want the best dark roast coffee, our dark roast coffee shop has the dark roast coffee beans you need. Buy dark roast coffee today!”
This is painful to read. Humans hate it, and modern search engines are smart enough to recognize it. This practice violates search quality guidelines. If their algorithm catches you doing this, they will penalize your site. Your page might get pushed to page ten of the search results, or it might get removed from the index entirely.
How has keyword density evolved in Google’s ranking algorithms?
To understand why we analyze word frequency today, we have to look at the history of search. The way search engines analyze text has changed completely over the last few decades.
The dark ages of 90s keyword stuffing
Back in the late 1990s and early 2000s, search engines were simple. They matched keywords literally. If someone searched for “cheap flight tickets,” the search engine looked for the page that contained that exact phrase the most times.
Web designers quickly figured out how to exploit this. They would copy and paste their target keyword thousands of times at the bottom of their pages. To hide it from human eyes, they made the text white and placed it on a white background. Humans saw a clean page, but search bots saw a huge wall of keywords. It worked for years, but it made search results terrible.
The arrival of LSI, TF-IDF, and modern vector models
Google changed the game by introducing updates that penalize keyword stuffing and reward natural writing. They moved away from simple keyword matching and began focusing on semantic search.
First came Latent Semantic Indexing (LSI). This mathematical technique helped search engines identify related terms. If your page is about “Apple,” LSI helps the search engine figure out if you mean the fruit (by looking for orchard, pie, juice) or the tech company (by looking for iPhone, MacBook, stock).
Later, Google integrated deep learning models like BERT and MUM. These models analyze sentences as whole ideas rather than individual words. They look at the relationships between words and understand context, synonyms, and intent. This means you do not have to repeat your target keyword twenty times to rank. You can use synonyms and natural phrasing, and the search engine will still understand your topic.
What is TF-IDF and how does it differ from raw keyword density?
If you want to understand how modern search crawlers analyze text, you need to understand TF-IDF. It stands for Term Frequency-Inverse Document Frequency. It is a statistical formula that measures how important a word is to a document in a collection of documents.
Unlike raw keyword density, which only looks at a single page, TF-IDF compares your page to millions of other pages on the web.
Term Frequency (TF) explained simply
Term Frequency measures how often a word appears in a specific document. It is very similar to keyword density, but it is often scaled. The simplest way to calculate TF is:
$$\text{TF}(t, d) = \frac{\text{Number of times term } t \text{ appears in document } d}{\text{Total number of words in document } d}$$
If the word “coffee” appears 5 times in a 100-word document, its TF is 0.05.
Inverse Document Frequency (IDF) and why it matters
Some words (like “the,” “is,” “and,” or “of”) appear in almost every document on the internet. If a search engine only looked at Term Frequency, these common words would be flagged as the most important words on every page. That is useless.
Inverse Document Frequency solves this by measuring how common a word is across all documents in a system. If a word appears in every document, its IDF score is very low. If a word is rare, its IDF score is very high.
The formula for IDF is:
$$\text{IDF}(t) = \ln\left( \frac{\text{Total number of documents}}{\text{Number of documents containing term } t} \right)$$
By multiplying TF by IDF, we get a score that highlights words that are frequent in our document but relatively rare in general. This tells the search engine exactly what makes our page unique.
Let us walk through a mathematical example with three documents:
- Document 1: “baking chocolate cookies is fun”
- Document 2: “chocolate cake is sweet and delicious”
- Document 3: “making cookies requires sugar and chocolate”
We want to calculate the TF-IDF score for two words in Document 3: “chocolate” and “cookies”.
First, look at the word “chocolate” in Document 3:
- Term Frequency (TF): The total number of words in Document 3 is 6. The word “chocolate” appears exactly 1 time. $$\text{TF(“chocolate”, Document 3)} = \frac{1}{6} \approx 0.167$$
- Inverse Document Frequency (IDF): Our database has 3 documents. The word “chocolate” appears in all 3 documents. $$\text{IDF(“chocolate”)} = \ln\left( \frac{3}{3} \right) = \ln(1) = 0$$
- TF-IDF: $$\text{TF-IDF(“chocolate”, Document 3)} = 0.167 \times 0 = 0$$
Because “chocolate” appears in every document, its TF-IDF score is zero. The algorithm realizes this word is too common to tell us anything unique about Document 3.
Now, calculate the score for the word “cookies” in Document 3:
- Term Frequency (TF): The word “cookies” appears 1 time in Document 3. $$\text{TF(“cookies”, Document 3)} = \frac{1}{6} \approx 0.167$$
- Inverse Document Frequency (IDF): Our database has 3 documents. The word “cookies” appears in Document 1 and Document 3, meaning it is in 2 documents total. $$\text{IDF(“cookies”)} = \ln\left( \frac{3}{2} \right) = \ln(1.5) \approx 0.405$$
- TF-IDF: $$\text{TF-IDF(“cookies”, Document 3)} = 0.167 \times 0.405 \approx 0.068$$
The word “cookies” gets a higher TF-IDF score than “chocolate” because it is a rarer word in our database. This is how search engines identify the signature terms of your content.
How do you write code to count word frequency?
If you want to build your own text tools, writing a word frequency counter in JavaScript is a fantastic project. It helps you understand how text parsing works under the hood.
A basic JavaScript word counter implementation
Here is a simple script. This function takes a string of text, cleans it up, splits it into individual words, and counts how often each word appears.
function countWordFrequency(text) {
if (!text || typeof text !== 'string') return [];
const cleanText = text.toLowerCase();
const rawWords = cleanText.split(/\s+/);
const frequencyMap = {};
rawWords.forEach(word => {
if (word.trim() === '') return;
frequencyMap[word] = (frequencyMap[word] || 0) + 1;
});
return Object.entries(frequencyMap)
.sort((a, b) => b[1] - a[1]);
}
Handling edge cases (punctuation, case insensitivity, stop words)
To make a word counter useful, we need to strip out punctuation and ignore common words (called stop words) like “a,” “an,” “the,” “is,” and “on.” These words do not add semantic meaning to our analysis.
Here is an improved version of our script:
const STOP_WORDS = new Set([
'i', 'me', 'my', 'we', 'our', 'you', 'your', 'he', 'him', 'his', 'she',
'her', 'it', 'its', 'they', 'them', 'their', 'what', 'which', 'who',
'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be',
'been', 'being', 'have', 'has', 'had', 'do', 'does', 'did', 'a', 'an',
'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of',
'at', 'by', 'for', 'with', 'about', 'into', 'through', 'during', 'to', 'from'
]);
function cleanAndCountWords(text, removeStopWords = true) {
if (!text) return [];
const normalizedText = text
.toLowerCase()
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()?"'’]/g, ' ')
.replace(/\s+/g, ' ');
const words = normalizedText.split(' ');
const frequencyMap = {};
words.forEach(word => {
const trimmed = word.trim();
if (trimmed === '') return;
if (removeStopWords && STOP_WORDS.has(trimmed)) return;
if (trimmed.length < 2) return;
frequencyMap[trimmed] = (frequencyMap[trimmed] || 0) + 1;
});
return Object.entries(frequencyMap)
.sort((a, b) => b[1] - a[1]);
}
Advanced JavaScript script for phrase frequency (bigrams and trigrams)
Single words are only half the story. Often, search queries are phrases. A two-word phrase is called a bigram, and a three-word phrase is a trigram. Here is a script that extracts and counts phrase frequencies:
function getPhraseFrequency(text, phraseLength = 2) {
if (!text || phraseLength < 1) return [];
const words = text
.toLowerCase()
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()?"'’]/g, ' ')
.split(/\s+/)
.filter(w => w.length > 0);
const phraseMap = {};
for (let i = 0; i <= words.length - phraseLength; i++) {
const phraseArray = [];
for (let j = 0; j < phraseLength; j++) {
phraseArray.push(words[i + j]);
}
const phrase = phraseArray.join(' ');
phraseMap[phrase] = (phraseMap[phrase] || 0) + 1;
}
return Object.entries(phraseMap)
.sort((a, b) => b[1] - a[1]);
}
How does a word frequency counter help non-SEO writers?
You do not have to be an SEO expert to benefit from analyzing word frequency. If you write novels, emails, reports, or essays, analyzing your word counts can make you a much better writer.
Spotting your writing crutches
Every writer has crutch words. These are the words we lean on when we are drafting quickly. They do not add any value, but they make our sentences feel cluttered. Some common crutches include “very,” “basically,” “actually,” “literally,” “just,” and “really.”
If you paste your text into a frequency analyzer and see that “actually” appears frequently in a short essay, that is a warning sign. You can search for those crutch words and delete them or replace them with stronger verbs. Your writing will immediately feel punchier.
Auditing competitor content
If you are writing a blog post and want to beat a competitor, you can use word frequency analysis as a research tool.
Go to their page, copy their text, and paste it into a frequency counter. Look at the top nouns and verbs they use. This gives you a clear list of the subtopics they covered. You might find they use words like “cost,” “warranty,” and “installation” frequently. If your article does not mention those terms, you know exactly what sections you need to add to match their coverage.
| Comparison Feature | Raw Keyword Density | TF-IDF Score | Word Frequency Count |
|---|---|---|---|
| Focus | Percentage of a single word on one page | Relative uniqueness of a word across pages | Total occurrence count of each word on a page |
| Punctuation Handling | Usually ignored | Stripped during parsing | Must be cleaned manually in code |
| Stop Word Filtering | Included in total word count | Excluded from significance calculations | Can be filtered to show meaningful terms |
| Primary Use Case | Basic checks for spam prevention | Search engine algorithms ranking pages | Writers self-editing and finding crutch words |
How do you use the TextSorter word frequency counter tool?
If you do not want to write custom JavaScript every time you edit an article, you can use our free Word Frequency Counter. It is designed to make text refinement quick and painless.
Step-by-step text refining walkthrough
Here is how to run an audit on your text using the tool:
- Copy and paste your draft: Open the Word Frequency Counter and paste your text into the main input box.
- Enable Stop Words filtering: Check the filter options. By default, the tool will ignore common words like “and,” “but,” and “the.” This allows you to see the real topic words immediately.
- Adjust the minimum character length: Set the limit to 3 or 4 letters. This cleans up short typos, abbreviations, and single letters that clutter your report.
- Examine the frequency table: Look at the sorted list of words. Check your target keyword. Is it at the top? Is the percentage between 1% and 2%?
- Check for repetitive crutches: Scroll down and look for words like “just” or “very.” If they have a high count, go back to your text editor and trim them down.
- Switch to phrase view: Look at the 2-word and 3-word phrase tables. Check if your main target keyword phrases are showing up in a natural ratio.
- Edit in real-time: Make your edits directly in the input box. The tables update instantly so you can see your percentages shift as you rewrite.
The TextSorter tool runs calculations locally in your browser. No text is uploaded to a server. You can audit confidential drafts or reports without data privacy worries.
What is the ideal keyword density in 2026?
Let us get one thing straight, there is no magic number. If someone tells you that you must hit exactly 1.83% keyword density to rank on Google, they are making things up.
Google’s algorithms are far too complex for simple percentages. However, we can follow some general rules of thumb based on industry standards:
- Under 0.5%: Your keyword might not appear enough. The search engine might struggle to connect your page to the query.
- Between 1% and 2%: This is the sweet spot. It is enough to show relevance without making the copy look unnatural.
- Between 2% and 3%: This is acceptable for technical topics where synonyms are hard to find, but keep an eye on readability.
- Over 3%: This is entering the danger zone. You run the risk of looking spammy. Read your text out loud. If it sounds repetitive, cut some keywords.
The best strategy is to write for humans first. Write naturally, explain your topic thoroughly, and cover related subtopics. Once you finish your first draft, paste it into the frequency counter to check your work. Tweak any areas that look unbalanced, remove your crutch words, and then hit publish.