(JavaScript programming Example for Beginners)
JavaScript Program to Count the Number of Vowels in a String
In this example, you will learn to write a JavaScript program that counts the number of vowels in a string.
The five letters a, e, i, o and u are called vowels. All other alphabets except these 5 vowels are called consonants.
Example 1: Count the number of Vowels using Regex
// program to count the number of vowels in a string
function countVowel(str) {
// find the count of vowels
let count = str.match(/[aeiou]/gi).length;
// return number of vowels
return count;
}
// take input
let string = prompt('Enter a string: ');
let result = countVowel(string);
console.log(result);
Output
Enter a string: JavaScript program 5
In the above program, the user is prompted to enter a string and that string is passed to the countVowel()
function.
- The regular expression (RegEx) pattern is used with the
match()
method to find the number of vowels in a string. - The pattern
/[aeiou]/gi
checks for all the vowels (case-insensitive) in a string. Here,
str.match(/[aeiou]/gi);
gives [“a”, “a”, “i”, “o”, “a”] - The
length
property gives the length of the vowels present.
Example 2: Count the number of Vowels using for Loop
// program to count the number of vowels in a string
// defining vowels
const vowels = ["a", "e", "i", "o", "u"]
function countVowel(str) {
// initialize count
let count = 0;
// loop through string to test if each character is a vowel
for (let letter of str.toLowerCase()) {
if (vowels.includes(letter)) {
count++;
}
}
// return number of vowels
return count
}
// take input
let string = prompt('Enter a string: ');
let result = countVowel(string);
console.log(result);
Output
Enter a string: JavaScript program 5
In the above example,
- All the vowels are stored in a
vowels
variable. - Initially, the value of the
count
variable is 0. - The
for...of
loop is used to iterate over all the characters of a string. - The
toLowerCase()
method converts all the characters of a string to lowercase. - The
includes()
method checks if the vowel variable contains any of the characters of the string. - If any character matches, the value of
count
is increased by 1.
JS Example for Beginners: JavaScript Program to Count the Number of Vowels in a String
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.