Open In App

How to get a number of vowels in a string in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

The Approach of this article is to return the number of vowels in a string using Javascript. A vowel is also a letter that represents a sound produced in this way: The vowels in English are a, e, i, o, u.  

Example:

Input:GeeksForGeeks
Output: 5
Input: Hello Geeks
Output: 4

Examples of getting a number of vowels in a string in JavaScript

1. Using for loop

Here we create a user-defined function called “getvowels()” which reads a string and compares it with the list of vowels. It compares each character of a string with vowels. When the vowels are matched it will increase the value of the vowelsCount.

Example: The below code will illustrate the approach.

JavaScript
function getVowels(string) {
    let Vowels = "aAeEiIoOuU";
    let vowelsCount = 0;
    for (let i = 0; i < string.length; i++) {
        if (Vowels.indexOf(string[i]) !== -1) {
            vowelsCount += 1;
        }
    }
    return vowelsCount;
}
console.log(
    "The Number of vowels in -" +
        " A Computer Science Portal for Geeks:" +
        getVowels(
            "A Computer Science Portal for Geeks"
        )
);

Output
The Number of vowels in - A Computer Science Portal for Geeks:12

2. Using Regex expression

regular expression is a sequence of characters that forms a search pattern. The search pattern can be used for text search and text to replace operations.

Example: In this example we are using Regex expression.

JavaScript
function vowelCount(str) {
    const vowelRegex = /[aeiou]/gi; 
    const strMatches = str.match(vowelRegex);

    if (strMatches) {
        return strMatches.length;
    } else {
        return 0; 
    }
}
const string = "Geeksforgeeks";
const len = vowelCount(string);
console.log("Number of vowels:", len);

Output
Number of vowels: 5

3.Using reduce() and indexOf() method

This approach uses the reduce() method to iterate through each character in the input string. The indexOf method is then used to check if the current character is a vowel. If it is, the count is incremented. The reduce method accumulates the count and returns the final result.

Example: In this example, we are using reduce and indexOf methods.

JavaScript
function countVowelsReduce(str) {
    const vowels = "aeiouAEIOU";
    return str
        .split("")
        .reduce(function (count, char) {
            return vowels.indexOf(char) !== -1
                ? count + 1
                : count;
        }, 0);
}
const result = countVowelsReduce("Hello, World!");
console.log(result);

Output
3

4.Using split() and Array.includes

Count vowels in a string by splitting it into characters, then iterating over each character. Increment count if it’s a vowel using Array.includes() method. Return count.

Example: below example uses the above explained approach.

JavaScript
function countVowels(str) {
    const vowels = [
        "a",
        "e",
        "i",
        "o",
        "u",
        "A",
        "E",
        "I",
        "O",
        "U",
    ];
    let count = 0;

    str.split("").forEach((char) => {
        if (vowels.includes(char)) {
            count++;
        }
    });

    return count;
}

const str = "Welcome to GeeksForGeeks";
console.log(countVowels(str));

Output
9


Last Updated : 15 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads