If you have a String object in JavaScript and you want to know the number of words it has, you can achieve this in various ways, lets see some of the code examples.
Example 1: Reading textarea data and finding the number of words in it.
<textarea id="myData" onBlur="getWordCount();"></textarea>
<div id="count"></div>
<script>
function getWordCount() {
var myString = document.getElementById("myData").innerHTML;
document.getElementById("count").innerHTML = myString.split(" ").length;
}
/<script>
Output:

As you can see in the above example we have called a function onBlur of a textarea and read the text as a variable and then called two operations over it. First, we have done split based on space to get the array of words and then used length to get the count.
In the first example there are many ways we can go wrong. It's always better to use the trim() function along with the first example as there might be cases where the first and the last text in the string are spaces and we may get the wrong count, also what if there are multiple spaces between two words.
Example 2: Using regular expressions (RegEx)
Using regular expression is another way of knowing the number of words in a string, lets see an example,
var string = 'This is some random String to get its connt';
console.log(string.match(/\w+/g).length);
Output:
9
JSFiddle: https://jsfiddle.net/u80fx3pa/Have Questions? Post them here!
- How to get UTC (GMT) time using JavaScript
- Submit html form on dropdown menu value selection or change using javascript
- How to detect Browser and Operating System Name and Version using JavaScript
- JavaScript : Get current page address
- [javaScript] Convert text case to lowercase
- How to check if a String contains substring or a word using javaScript
- Writing your first Hello, World! 🌍 JavaScript code Tutorial
- JavaScript: Check if variable is a number
- How to send email from JavaScript HTML using mailto
- Meaning of javascript:void(0) explained with example
- How to get query string in JavaScript HTML location.search
- Javascript convert text case from uppercase to lowercase
- Loading previous page using html button using JavaScript
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- How to get current URL Location using Javascript HTML
- Detect if Cookies are enabled using JavaScript
- Write javaScript code in Swedish using FikaScript
- Add Animated Scrolling to Html Page Title Script
- JavaScript: Convert an Image into Base64 String
- Remove items from JavaScript array
- Send Extra Data with Ajax Get or Post Request
- How to Print from JavaScript HTML using window.print
- Get Device Screen Width and Height using javaScript
- Examples: Convert String to int in JavaScript
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- Change the default download location for Mac Safari - MacOS
- Text Case Converter - Convert to Upper/Lower/Proper/Random Case - Tools
- NOTE: This project contains resource errors, so aapt did not succeed, which can cause rendering failures. Fix resource problems first. - Android
- Fix - 412 Cookies Are Disabled error in Exchange Admin Center when you click hybrid - Microsoft
- Use your iPhone microphone as a mic on macOS Ventura - MacOS
- [Fix] Microsoft Teams a JavaScript error occurred in the main process Error - Teams
- How to change Android Titlebar theme color from Purple - Android
- Convert SQL to CSV in Notepad++ - NotepadPlusPlus