Count Vowels in String in JavaScript
Count vowels in a string in JavaScript with loops, filter, reduce and regex. Also counts each vowel on its own, with a clear explanation and output.
1// Method 1: Using for loop2function countVowels1(str) {3 let count = 0;4 let vowels = 'aeiouAEIOU';56 for (let i = 0; i < str.length; i++) {7 if (vowels.includes(str[i])) {8 count++;9 }10 }11 return count;12}1314console.log("Hello World:", countVowels1("Hello World"));1516// Method 2: Using for...of loop17function countVowels2(str) {18 let count = 0;19 let vowels = ['a', 'e', 'i', 'o', 'u'];2021 for (let char of str.toLowerCase()) {22 if (vowels.includes(char)) {23 count++;24 }25 }26 return count;27}2829console.log("\nUsing for...of:", countVowels2("JavaScript"));3031// Method 3: Using filter32function countVowels3(str) {33 let vowels = ['a', 'e', 'i', 'o', 'u'];34 return str.toLowerCase()35 .split('')36 .filter(char => vowels.includes(char))37 .length;38}3940console.log("Using filter:", countVowels3("Programming"));4142// Method 4: Using match with regex43function countVowels4(str) {44 let matches = str.match(/[aeiou]/gi);45 return matches ? matches.length : 0;46}4748console.log("\nUsing regex:", countVowels4("Hello World"));4950// Method 5: Using reduce51function countVowels5(str) {52 let vowels = ['a', 'e', 'i', 'o', 'u'];53 return str.toLowerCase()54 .split('')55 .reduce((count, char) => {56 return vowels.includes(char) ? count + 1 : count;57 }, 0);58}5960console.log("Using reduce:", countVowels5("Education"));6162// Method 6: Count each vowel separately63function countEachVowel(str) {64 str = str.toLowerCase();65 return {66 a: (str.match(/a/g) || []).length,67 e: (str.match(/e/g) || []).length,68 i: (str.match(/i/g) || []).length,69 o: (str.match(/o/g) || []).length,70 u: (str.match(/u/g) || []).length71 };72}7374console.log("\nEach vowel count:", countEachVowel("Hello World"));
Sample 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 }In this one
- How to test each character against a vowel list
- Why lowercasing matters
- How regex match with g and i flags works
- How to handle a null match safely
How it works
- 1
countVowels1 finds 3 vowels in "Hello World".
- 2
for...of finds 3 in "JavaScript".
- 3
filter finds 3 in "Programming".
- 4
match finds 3 in "Hello World".
- 5
reduce finds 5 in "Education".
- 6
countEachVowel returns e: 1 and o: 2.
Edge cases
No vowels
"rhythm".match(/[aeiou]/gi) returns null.
The code returns 0 instead of reading null.length.
Letter y
"sky" has 0 vowels here.
Add y to the pattern if your rules count it.
Accented vowels
"café" counts a but not é.
Only plain a, e, i, o, u are matched.
What's going on
This program counts how many vowels a string has. Vowels here are a, e, i, o and u. Y is not in the set, on purpose. It keeps the rule simple.
Method 1 loops over each character and checks it against 'aeiouAEIOU'. Both cases are in the list, so no lowercasing is needed. "Hello World" has e, o, o, so the count is 3.
Method 2 lowercases the string first and loops with for...of. Doing toLowerCase first means the vowel list only needs small letters. If you forget it, capital vowels like the A in "Apple" are missed.
Method 3 splits into characters, uses filter to keep vowels, and reads the length of what is left.
Method 4 uses a regular expression: str.match(/[aeiou]/gi). [aeiou] matches any one vowel. g finds all matches, not just the first. i means ignore case, so you can skip toLowerCase. match returns an array of matches, or null if there are none. That is why the code writes matches ? matches.length : 0. Without that check, a word with no vowels like "rhythm" would crash on null.length.
Method 5 uses reduce to add 1 for each vowel. "Education" has 5 vowels.
Method 6 counts each vowel separately and returns an object. (str.match(/a/g) || []) uses an empty array when there is no match, so length is 0.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
How many vowels are in "Banana"?
Pick an answer to check it.
Question 2
What does "xyz".match(/[aeiou]/g) return?
Pick an answer to check it.
Questions people ask
What does the i flag do?
It makes the regex ignore case, so A and a both match.
Why might match return null?
When nothing matches. Always check before reading length.
How do I count consonants?
Count letters a to z, then subtract the vowel count.