
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the number of words in a string JavaScript
We are required to write a JavaScript function that takes in a string of any length. The function should then count the number of words in that string.
Example
const str = 'THis is an example string'; const findWords = (str = '') => { if(!str.length){ return 0; }; let count = 1; for(let i = 0; i < str.length; i++){ if(str[i] === ' '){ count++; }; }; return count; }; console.log(findWords(str));
Output
And the output in the console will be −
5
- Related Articles
- Finding duplicate "words" in a string - JavaScript
- Finding number of spaces in a string JavaScript
- Finding top three most occurring words in a string of text in JavaScript
- Finding words in a matrix in JavaScript
- How to count a number of words in given string in JavaScript?
- Replace words of a string - JavaScript
- Reversing the order of words of a string in JavaScript
- Finding the ASCII score of a string - JavaScript
- Finding mistakes in a string - JavaScript
- Finding the greatest and smallest number in a space separated string of numbers using JavaScript
- Swapping adjacent words of a String in JavaScript
- Reversing the even length words of a string in JavaScript
- Finding n most frequent words from a sentence in JavaScript
- Reversing words in a string in JavaScript
- Reverse the words in the string that have an odd number of characters in JavaScript

Advertisements