
- 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
Counting number of words in a sentence in JavaScript
We are required to write a JavaScript function that takes in a string. Our function is supposed to count the number of alphabets (uppercase or lowercase) in the array.
For example − If the input string is −
const str = 'this is a string!';
Then the output should be −
13
Example
The code for this will be −
const str = 'this is a string!'; const isAlpha = char => { const legend = 'abcdefghijklmnopqrstuvwxyz'; return legend.includes(char); }; const countAlphabets = (str = '') => { let count = 0; for(let i = 0; i < str.length; i++){ if(!isAlpha(str[i])){ continue; }; count++; }; return count; }; console.log(countAlphabets(str));
Output
And the output in the console will be −
13
- Related Articles
- Counting adjacent pairs of words in JavaScript
- Arranging words by their length in a sentence in JavaScript
- Reverse all the words of sentence JavaScript
- Replace all occurrence of specific words in a sentence based on an array of words in JavaScript
- Finding n most frequent words from a sentence in JavaScript
- Rearrange Words in a Sentence in C++
- Number of letters in the counting JavaScript
- Counting number of vowels in a string with JavaScript
- Count words in a sentence in Python program
- Count palindrome words in a sentence in C++
- Counting divisors of a number using JavaScript
- Counting number of 9s encountered while counting up to n in JavaScript
- Constructing a sentence based on array of words and punctuations using JavaScript
- Counting the number of redundant characters in a string - JavaScript
- Python program to count words in a sentence

Advertisements