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:

JavaScript
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

Similar Reads

Using includes() Method

In this approach, we are using the includes() methods in the JavaScript. This method is used to check whether the input element is present in the given array or string. If it is present then, it will return true else false....

Using test() Method

In this apporach, we have used the test method. This test mehod works with the regex or regular pattern. This method checks whehter the given input follows the regex pattern, if it follows then it gives True as result else False is been printed....

Using indexOf() Method

In this apporach, we are using the indexOf() method of JavaScript. Here, the indexOf() method check if the input is present in the string It returns the index of the first occurrence of the character the string, or -1 if the character is not found....

Using if-else Statements

In this approach, we are using the condtional if-else block in the JavaScript. Here, we have specified multiple conditions in the if block. If the input character is same or equal to vowel characters then True result is been printed else, False is been printed....

Using a Switch Statement

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`....

Using a Set for O(1) Lookups

Using a Set for O(1) lookups involves storing vowels in a Set, enabling efficient membership checks. This approach leverages the Set’s average constant-time complexity for checking if a character (converted to lowercase) is present, making it both clean and performant....