- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get a number of vowels in a string in JavaScript?
Calculating number of vowels in a string
Vowels in English language are a,e,i,o and u. Make sure that, in any string these vowels can be both cases ( either small or capital).
Debrief
The following example, using a user defined function called 'noOfVowels()', reads an input string and compares that string with another string which contains only vowels( 'aAeEiIoOuU'). It takes the help of indexOf() method to proceed the task.
The indexOf() method displays index of a character whenever the character is common to both the strings, in unmatched case it displays '-1' as the output. Here it compares each and every character of the input string to the vowel string and whenever vowels got matched, it internally increments a user defined variable called "vowelsCount", which is initially 0. Eventually, the value in the "vowelsCount" is displayed as the output.
Example
<html> <body> <script> function noOfVowels(string) { var listOfVowels = 'aAeEiIoOuU'; var vowelsCount = 0; for(var i = 0; i < string.length ; i++) { if (listOfVowels.indexOf(string[i]) !== -1) { vowelsCount += 1; } } return vowelsCount; } document.write(noOfVowels("Tutorix is one of the best e-platforms")); </script> </body> </html>
Output
12
- Related Articles
- Counting number of vowels in a string with JavaScript
- How to get a string representation of a number in JavaScript?
- How to get a number value of a string in Javascript?
- Reversing vowels in a string JavaScript
- How to Count the Number of Vowels in a string using Python?
- Return Vowels in a string in JavaScript
- How to count number of vowels and consonants in a string in C Language?
- C# Program to count number of Vowels and Consonants in a string
- Python program to count number of vowels using set in a given string
- How to get the length of a string in JavaScript?
- Reverse Vowels of a String in Python
- Reverse Vowels of a string in C++
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- How to get the length of a Number in JavaScript?
