Count words frequency using Javascript

Search for a command to run...

No comments yet. Be the first to comment.
Note: Please note that experimental code ahead. Only tested in few test cases. Problem You are given an array of desired filenames in the order of their creation. Since two files cannot have equal names, the one which comes later will have an additio...
We’ve tried out lots of apps over time, and honestly—not all of them do what we really need. That’s why I’m writing this blog post: to share some of the challenges we’ve noticed with the Shopify Forms app and hear what others think too. If you’ve fou...
Hey everyone, Dorothi Viki here. For the last decade, I’ve been in the digital marketing trenches. I’ve managed budgets big and small, and I’ve seen firsthand how crucial it is to know what people are saying about you online. When you’re starting out...
If you’re a small business owner, freelancer, or consultant, your website is your storefront. But traditional website development can be expensive, slow, and intimidating. Platforms like SpreadSimple remove these barriers by allowing you to turn Goog...

For SEO professionals, content marketers, freelancers, and consultants, keyword research is non-negotiable. It’s the foundation of any effective search strategy. However, with so many tools behind paywalls, finding a reliable, high-quality, free Goog...
With the rise of AI agents and Claude-specific development environments, understanding Claude Code has become essential for modern developers, freelancers, and consultants. Whether you're building Multi-Component Programs (MCPs) or designing autonomo...
In this programming tutorial, you'll see a javascript function to count the frequency of words. Instead of using a third-party npm package, I had to create a custom script to return the number of words as JS Object for a CLI project.
Here is the solution code:
countWords(text) {
//Edge case: an empty array
if (text.length === 0) {
return {};
}
//Normalize words
text = text.toLowerCase();
/* regular expression to tokenise by word while ignoring punctuation. */
const pattern = /\w+/g;
text = text.match(pattern);
let wordsOccurence = {};
/* Output an object with results. */
wordsOccurence = text.reduce((wordsOccurenceMap, word) => {
if (Object.hasOwnProperty.call(wordsOccurence, word))
wordsOccurence[word] = 1;
else wordsOccurence[word] += 1;
return wordsOccurence;
}, {});
return wordsOccurence;
}
You can see that the text we passed to the functions can be normalized by converting to lower case. This is fully customizable that is if you can consider the upper case and lower case separately by updating the code.
Another main step is to ignore the punctuations or other symbols from using regular expressions. In our current scenario, we're only taking alphabets for consideration by ignoring all others.
I'll suggest using something like Regex101 for testing out your regular expressions.
Here, we are using JS reduce function to create JS object of words count.
{
'Hello':3,
'World':1
}
If you like to know more about JS reduce function, please check out these: JS Array reduce() and How to Use Array Reduce Method in JavaScript.
We created a tool to get the word frequency, please check here.
That's all for now. I hope you got the javascript code solution for creating word frequency. Please like and share this article. Feel free to use the comment option below to give your opinions.
Let's Connect with Makeinfo. Our vision is to create a productive workflow using existing solutions. If you follow us, then you can definitely notice that we are trying our best efforts to make productive solutions with few clicks.