
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- Replace words of a string - JavaScript
- Reversing the order of words of a string in JavaScript
- Finding the ASCII score of a string - JavaScript
- How to count a number of words in given string in JavaScript?
- Reversing the even length words of a string in JavaScript
- Swapping adjacent words of a String in JavaScript
- Java Program to count the number of words in a String
- C# program to count the number of words in a string
- Finding the length of words in a given Pandas series
- Finding the largest prime factor of a number in JavaScript
- Finding place value of a number in JavaScript
Advertisements