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 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 More infoAdvertise with us M manaschhabra2 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to add float numbers using JavaScript ? Given two or more numbers the task is to get the float addition in the desired format with the help of JavaScript. There are two methods to solve this problem which are discussed below: Table of Content Using parseFloat() and toFixed() method Using parseFloat() and Math.round() method Using Number() 2 min read Calculate current week number in JavaScript Calculating the current week number involves determining which week of the year the current date falls into. The method of calculation can vary slightly depending on the rules you follow, such as which day starts the week (Sunday or Monday) and how the first week of the year is defined.For example:J 2 min read How to Convert a Float Number to the Whole Number in JavaScript? Given a float number and the task is to convert a float number to a whole number using JavaScript. Below are various methods to convert float numbers to whole numbers in JavaScript:Table of ContentMath.floor (floating argument)Math.ceil (floating argument) Math.round (floating argument)Math.trunc (f 4 min read How to convert Number to Boolean in JavaScript ? We convert a Number to Boolean by using the JavaScript Boolean() method and double NOT operator(!!). A JavaScript boolean results in one of two values i.e. true or false. However, if one wants to convert a variable that stores integer â0â or â1â into Boolean Value i.e. "false" or "true". Below are 2 min read Convert a Number to a String in JavaScript These are the following ways to Convert a number to a string in JavaScript:1. Using toString() Method (Efficient and Simple Method)This method belongs to the Number.Prototype object. It takes an integer or a floating-point number and converts it into a string type.JavaScriptlet a = 20; console.log(a 1 min read Convert a negative number to positive in JavaScript In this article, we will see how we can convert a negative number to a positive number in JavaScript by the methods described below. Below are the methods to convert a negative number to a positive in JavaScript: Table of Content Multiplying by -1Using Math.abs()adding a minus signFlipping the bitUs 4 min read Check a Number is Prime or Not Using JavaScript A prime number is a whole number greater than 1, which has no positive divisors other than 1 and itself. In other words, prime numbers cannot be formed by multiplying two smaller natural numbers. For example:2, 3, 5, 7, 11, and 13 are prime numbers.4, 6, 8, 9, and 12 are not prime numbers because th 5 min read JavaScript - Convert a Number into JS Array You have a number, like 12345, and you need to convert it into an array where each element represents a digit of the number. For example, 12345 should become [1, 2, 3, 4, 5]. How can you achieve this in JavaScript?In JavaScript, there are various ways to transform a number into an array of its digit 3 min read How to generate a n-digit number using JavaScript? The task is to generate an n-Digit random number with the help of JavaScript. You can also generate random numbers in the given range using JavaScript. Below are the approaches to generate a n-digit number using JavaScript: Table of Content Using Math.random()Math.random() Method and .substring() Me 2 min read How to get decimal portion of a number using JavaScript ? Given a float number, The task is to separate the number into integer and decimal parts using JavaScript. For example, a value of 15.6 would be split into two numbers, i.e. 15 and 0.6 Here are a few methods discussed. These are the following methods: Table of Content Javascript String split() Method 3 min read Like