Open In App

JavaScript - How to Get a Number of Vowels in a String?

Last Updated : 26 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are the various methods to get the number of vowels in a string using JavaScript.

1. Using a for Loop

This is the most basic and beginner-friendly approach. It uses a loop to iterate over each character and checks if it is a vowel.

JavaScript
const cVowels = (s) => {
    const vowels = "aeiouAEIOU";
    let count = 0;

    for (const char of s) {
        if (vowels.includes(char)) {
            count++;
        }
    }

    return count;
};

const s = "Hello World";
console.log(cVowels(s)); 

Output
3
  • Define a string containing all vowels.
  • Loop through the input string and check if each character is a vowel using includes().
  • Increment the counter for every match.

2. Using Regular Expression

Using a regex pattern is a compact and efficient way to count vowels in a string.

JavaScript
const cVowels = (s) => {
    const matches = s.match(/[aeiou]/gi);
    return matches ? matches.length : 0;
};

const s = "Hello World";
console.log(cVowels(s));

Output
3
  • The regex /[aeiou]/gi matches all vowels (a, e, i, o, u) in a case-insensitive manner (i) and globally (g).
  • Use the match method to find all matches.
  • Return the length of the matches array or 0 if no matches are found.

3. Using reduce() and indexOf() Methods

This method uses the reduce() function to iterate over the string while checking if a character is a vowel.

JavaScript
const cVowels = (s) => {
    const vowels = "aeiouAEIOU";
    return [...s].reduce((count, char) => 
        vowels.indexOf(char) !== -1 ? count + 1 : count, 0);
};

const s = "Hello World";
console.log(cVowels(s)); 

Output
3
  • Spread the string into an array of characters.
  • Use reduce() to accumulate the count.
  • Check if each character exists in the vowels string using indexOf.

4. Using split() and Array.includes() Methods

This method uses split() to create an array of characters and includes() to check for vowels.

JavaScript
const cVowels = (s) => {
    const vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];
    return s.split("").filter(char => vowels.includes(char)).length;
};

const s = "Hello World";
console.log(cVowels(s));

Output
3
  • Split the string into an array of characters.
  • Use filter() to keep only characters that are vowels.
  • Return the length of the filtered array.

5. Using a Map

This approach uses a Map to store vowels as keys for faster lookup.

JavaScript
const cVowels = (s) => {
    const vowelsMap = new Map([
        ["a", true], ["e", true], ["i", true], ["o", true], ["u", true],
        ["A", true], ["E", true], ["I", true], ["O", true], ["U", true]
    ]);

    let count = 0;
    for (const char of s) {
        if (vowelsMap.has(char)) {
            count++;
        }
    }
    return count;
};

const s = "Hello World";
console.log(cVowels(s));

Output
3
  • Create a Map with all vowels as keys for quick lookup.
  • Iterate through the string using a for...of loop.
  • Increment the count for each vowel found in the Map.

Which Approach Should You Use?

ApproachWhen to Use
For LoopBest for beginners; simple and easy to understand.
Regular ExpressionIdeal for compact code and performance; great for complex patterns.
Reduce and IndexOfUse when you prefer a functional programming style.
Split and Array.includesBest for readability and working with arrays.
MapSuitable for large datasets requiring faster lookups.

Next Article

Similar Reads