We can easily check if a string contains vowels in JavaScript by either using a for loop to check each character individually to see if it is a vowel or not, or we can use the JavaScript match() method.
Let’s take a look at both methods below. We will start with using a for loop.
function containsVowel(theString){
for( var i=0; i
We created a function above called containsVowel. It takes one parameter which is the string we want to check for vowels. We simply loop through all the characters in the string and use the indexOf() and toLowerCase() methods to help us see if any character in the string will equal a vowel, aeiou. The function will return true if it does, and false if not.
Now let's put this function to the test with some examples.
function containsVowel(theString){
for( var i=0; i
When working with strings, it can be useful to know if there are any vowels contained in a string variable.
In JavaScript, we can easily get if a string contains vowels in a string by looping over each character of the string and seeing if it is a vowel or not.
The vowels include "a","e","i","o", and "u"
.
Let's take a look at another way to find if a string contains vowels that doesn't use as much code.
Checking if a String Contains Vowels in JavaScript Using the match() Method
Another way we can check to see if a string contains vowels in JavaScript is by using the JavaScript match() method along with a regex expression. Here is the code below.
function containsVowel(theString) {
var numVowels = theString.match(/[aeiou]/ig);
if( numVowels != null ){
return true;
} else {
return false;
}
};
In the code above, the match() method will check the string for any vowels. It will return all of the matching vowels it finds. If it does not find any, it will return null. So we simply have to check if the match() method returns null, and if it doesn't, we know that there are vowels in the string.
Let's test our function with the same examples from above.
function containsVowel(theString) {
var numVowels = theString.match(/[aeiou]/ig);
if( numVowels != null ){
return true;
} else {
return false;
}
};
console.log(containsVowel("Hello World!"));
console.log(containsVowel("This is a string with some words."));
console.log(containsVowel("What's up?"));
console.log(containsVowel("Brrrr"));
console.log(containsVowel("All"));
#Output:
true
true
true
false
true
Counting the number of times Each Vowel Appears in a String Using JavaScript
The example above is useful if you want to check if any vowel is in a string. We can also use JavaScript to check if each of the 5 vowels appear in a string, and how many times each does.
To do this, we use the match() method as we did in our second example, but check for each individual vowel instead of them all. We will store each number in an object, with each key, value pair in the object representing the vowel, and the number of times it appears in the string.
Below is our JavaScript function which will return the number of vowels in the string.
function numEachVowel(theString){
var vowelsObj = {};
var numberOfAs = theString.match(/[a]/ig);
if( numberOfAs == null ){
vowelsObj.a = 0;
} else {
vowelsObj.a = numberOfAs.length;
}
var numberOfEs = theString.match(/[e]/ig);
if( numberOfEs == null ){
vowelsObj.e = 0;
} else {
vowelsObj.e = numberOfEs.length;
}
var numberOfIs = theString.match(/[i]/ig);
if( numberOfIs == null ){
vowelsObj.i = 0;
} else {
vowelsObj.i = numberOfIs.length;
}
var numberOfOs = theString.match(/[o]/ig);
if( numberOfOs == null ){
vowelsObj.o = 0;
} else {
vowelsObj.o = numberOfOs.length;
}
var numberOfUs = theString.match(/[u]/ig);
if( numberOfUs == null ){
vowelsObj.u = 0;
} else {
vowelsObj.u = numberOfUs.length;
}
return vowelsObj;
};
console.log(numEachVowel("Hello World!"));
console.log(numEachVowel("This is a string with some words."));
console.log(numEachVowel("What's up?"));
console.log(numEachVowel("Brrrr"));
#Output:
{a: 0, e: 1, i: 0, o: 2, u: 0}
{a: 1, e: 1, i: 4, o: 2, u: 0}
{a: 1, e: 0, i: 0, o: 0, u: 1}
{a: 0, e: 0, i: 0, o: 0, u: 0}
Hopefully this article has been useful for you to learn how to check if a string contains vowels in JavaScript.