Count Vowels in String

Program to count vowels in a string

JavaScriptBeginner
JavaScript
// 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"));

Output

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 }

This program demonstrates different methods to count vowels in a string.

Vowels

English vowels: a, e, i, o, u (and sometimes y)

Method 1: For Loop

Traditional approach:

javascript
for (let i = 0; i < str.length; i++) {
    if (vowels.includes(str[i])) {
        count++;
    }
}

Method 2: For...Of Loop

Cleaner iteration:

javascript
for (let char of str.toLowerCase()) {
    if (vowels.includes(char)) {
        count++;
    }
}

Method 3: Filter

Functional approach:

javascript
str.toLowerCase()
   .split('')
   .filter(char => vowels.includes(char))
   .length;

Method 4: Regular Expression

Using match():

javascript
str.match(/[aeiou]/gi);

Regex Flags:

  • g: Global (all matches)
  • i: Case-insensitive

Method 5: Reduce

Accumulate count:

javascript
str.split('').reduce((count, char) => {
    return vowels.includes(char) ? count + 1 : count;
}, 0);

Method 6: Count Each Vowel

Individual counts:

javascript
{
    a: (str.match(/a/g) || []).length,
    e: (str.match(/e/g) || []).length,
    // ...
}

Null Safety:

  • || []: Default to empty array if no match
  • Prevents error on null

When to Use:

  • Loop: Simple, learning

  • Filter: Functional, readable

  • Regex: Concise, powerful

  • Reduce: Accumulation pattern