- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 how many times a specific letter is appearing in a sentence in JavaScript
We are required to write a JavaScript function that finds how many times a specific letter is appearing in the sentence.
Example
The code for this will be −
const string = 'This is just an example string for the program'; const countAppearances = (str, char) => { let count = 0; for(let i = 0; i < str.length; i++){ if(str[i] !== char){ // using continue to move to next iteration continue; }; // if we reached here it means that str[i] and char are same // so we increase the count count++; }; return count; }; console.log(countAppearances(string, 'a')); console.log(countAppearances(string, 'e')); console.log(countAppearances(string, 's'));
Output
The output in the console −
3 3 4
- Related Articles
- Finding how many time a specific letter is appearing in the sentence using for loop, break, and continue - JavaScript
- Finding word starting with specific letter in JavaScript
- Finding missing letter in a string - JavaScript
- Finding number of occurrences of the element appearing most number of times in JavaScript
- Finding n most frequent words from a sentence in JavaScript
- Finding letter distance in strings - JavaScript
- Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively
- Finding the length of second last word in a sentence in JavaScript
- C program to count a letter repeated in a sentence.
- Finding the immediate next character to a letter in string using JavaScript
- Finding two prime numbers with a specific number gap in JavaScript
- How to Title Case a sentence in JavaScript?
- Counting how many times an item appears in a multidimensional array in JavaScript
- Finding the k-prime numbers with a specific distance in a range in JavaScript
- How To Check If The First Letter In A Specific Cell Is Capital In Excel?

Advertisements