JavaScript - How to Get a Number of Vowels in a String? Last Updated : 23 Jul, 2025 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 LoopThis 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)); Output3 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 ExpressionUsing 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)); Output3 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() MethodsThis 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)); Output3 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() MethodsThis 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)); Output3 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 MapThis 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)); Output3 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 UseFor 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. Comment M manaschhabra2 Follow Improve M manaschhabra2 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like