Number of vowels within an array in JavaScript

We are required to write a JavaScript function that takes in an array of strings, (they may be a single character or greater than that). Our function should simply count all the vowels contained in the array.

Example

Let us write the code −

const arr = ['Amy','Dolly','Jason','Madison','Patricia'];
const countVowels = (arr = []) => {
   const legend = 'aeiou';
   const isVowel = c => legend.includes(c.toLowerCase());
   let count = 0;
   arr.forEach(el => {
      for(let i = 0; i < el.length; i++){
         if(isVowel(el[i])){
            count++;
         };
      };
   });
   return count;
};
console.log(countVowels(arr));

Output

And the output in the console will be −

10

Method 2: Using Regular Expressions

We can also use regular expressions to match vowels more efficiently:

const arr = ['Amy','Dolly','Jason','Madison','Patricia'];
const countVowelsRegex = (arr = []) => {
   const vowelPattern = /[aeiou]/gi;
   let count = 0;
   arr.forEach(str => {
      const matches = str.match(vowelPattern);
      if(matches) {
         count += matches.length;
      }
   });
   return count;
};
console.log(countVowelsRegex(arr));
10

Method 3: Using Reduce and Filter

A more functional programming approach using reduce and filter:

const arr = ['Amy','Dolly','Jason','Madison','Patricia'];
const countVowelsFunctional = (arr = []) => {
   const vowels = 'aeiou';
   return arr.reduce((total, str) => {
      return total + str.toLowerCase()
                        .split('')
                        .filter(char => vowels.includes(char))
                        .length;
   }, 0);
};
console.log(countVowelsFunctional(arr));
10

Comparison

Method Performance Readability Best For
Loop with includes() Good High Beginners
Regular Expression Very Good Medium Pattern matching
Reduce and Filter Good High Functional programming

Conclusion

All three methods effectively count vowels in an array of strings. The loop method is most readable for beginners, while regex offers better performance for large datasets.

Updated on: 2026-03-15T23:19:00+05:30

799 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements