Count Vowels in String
Program to count vowels in a string
JavaScript Count Vowels in String Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Using for loop
function countVowels1(str) {
let count = 0;
let vowels = 'aeiouAEIOU';
for (let i = 0; i < str.length; i++) {
if (vowels.includes(str[i])) {
count++;
}
}
return count;
}
console.log("Hello World:", countVowels1("Hello World"));
// Method 2: Using for...of loop
function countVowels2(str) {
let count = 0;
let vowels = ['a', 'e', 'i', 'o', 'u'];
for (let char of str.toLowerCase()) {
if (vowels.includes(char)) {
count++;
}
}
return count;
}
console.log("\nUsing for...of:", countVowels2("JavaScript"));
// Method 3: Using filter
function countVowels3(str) {
let vowels = ['a', 'e', 'i', 'o', 'u'];
return str.toLowerCase()
.split('')
.filter(char => vowels.includes(char))
.length;
}
console.log("Using filter:", countVowels3("Programming"));
// Method 4: Using match with regex
function countVowels4(str) {
let matches = str.match(/[aeiou]/gi);
return matches ? matches.length : 0;
}
console.log("\nUsing regex:", countVowels4("Hello World"));
// Method 5: Using reduce
function countVowels5(str) {
let vowels = ['a', 'e', 'i', 'o', 'u'];
return str.toLowerCase()
.split('')
.reduce((count, char) => {
return vowels.includes(char) ? count + 1 : count;
}, 0);
}
console.log("Using reduce:", countVowels5("Education"));
// Method 6: Count each vowel separately
function countEachVowel(str) {
str = str.toLowerCase();
return {
a: (str.match(/a/g) || []).length,
e: (str.match(/e/g) || []).length,
i: (str.match(/i/g) || []).length,
o: (str.match(/o/g) || []).length,
u: (str.match(/u/g) || []).length
};
}
console.log("\nEach vowel count:", countEachVowel("Hello World"));Hello World: 3
Using for...of: 3
Using filter: 3
Using regex: 3
Using reduce: 5
Each vowel count: { a: 0, e: 1, i: 0, o: 2, u: 0 }Understanding Count Vowels in String
This program demonstrates different methods to count vowels in a string.
Vowels
Method 1: For Loop
Traditional approach:
for (let i = 0; i < str.length; i++) {
if (vowels.includes(str[i])) {
count++;
}
}
Method 2: For...Of Loop
Cleaner iteration:
for (let char of str.toLowerCase()) {
if (vowels.includes(char)) {
count++;
}
}
Method 3: Filter
Functional approach:
str.toLowerCase()
.split('')
.filter(char => vowels.includes(char))
.length;
Method 4: Regular Expression
Using match():
str.match(/[aeiou]/gi);
Regex Flags:
g: Global (all matches)i: Case-insensitiveMethod 5: Reduce
Accumulate count:
str.split('').reduce((count, char) => {
}, 0);
return vowels.includes(char) ? count + 1 : count;Method 6: Count Each Vowel
Individual counts:
{
a: (str.match(/a/g) || []).length,
e: (str.match(/e/g) || []).length,
}
// ...Null Safety:
|| []: Default to empty array if no matchWhen to Use:
-
Loop
: Simple, learning
-
Filter
: Functional, readable
-
Regex
: Concise, powerful
-
Reduce
: Accumulation pattern
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.