How to use a Switch Statement In Javascript
To check if a character is a vowel using a switch statement, convert the character to lowercase, then switch on it. Case statements include each vowel; return `true` if it matches, else `false`.
Example:
function isVowel(char) {
switch (char.toLowerCase()) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
default:
return false;
}
}
console.log(isVowel('a')); // Output: true
console.log(isVowel('b')); // Output: false
Output
true false
JavaScript Program to Check if a Character is Vowel or Not
In this article, we will check if the input character is a vowel or not using JavaScript. Vowel characters are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’. All other characters are not vowels.
Examples:
Input : 'u'
Output : True
Explanation: 'u' is among the vowel characters family (a,e,i,o,u).
Input : 'b'
Output : False
Explanation: 'b' is not among the vowel characters family (a,e,i,o,u).
Table of Content
- Using includes() Method
- Using test() Method
- Using indexOf() Method
- Using if-else Statements
- Using a Switch Statement
- Using a Set for O(1) Lookups