DEV Community

Алексей Спинов
Алексей Спинов

Posted on

Keyword Density Analysis in JavaScript — Find Your Page's Top Words

SEO keyword density tells you how often words appear on your page. Here's how to analyze it.

The Approach

  1. Fetch page HTML
  2. Remove noise (nav, footer, scripts, ads)
  3. Extract body text
  4. Count word frequency (exclude stop words)
  5. Calculate 2-word phrases (bigrams)
  6. Check target keyword placement

Stop Words

Filter out: the, a, an, is, are, was, in, on, at, to, for, of, with, by...

Output

{
  "url": "https://example.com",
  "totalWords": 1450,
  "topWords": [
    {"word": "scraping", "count": 15, "density": 1.03},
    {"word": "data", "count": 12, "density": 0.83}
  ],
  "topPhrases": [
    {"phrase": "web scraping", "count": 8, "density": 0.55}
  ]
}
Enter fullscreen mode Exit fullscreen mode

Target Keyword Check

For each target keyword, verify placement in:

  • Title tag
  • H1 heading
  • Meta description

I built a Keyword Density Analyzer on Apify — search knotless_cadence keyword-density.

Top comments (0)