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
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.
Syntax:
array.includes(searchElement[, index])Example: In this example, we will check if check if a character is vowel or not in javascript using includes() method.
let input = 'a';
let vowels =
['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
let result = vowels.includes(input);
console.log(result);
Output
true
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.
Syntax:
regexp.test(str)Example: In this example, we will check if check if a character is vowel or not in javascript using test() method.
let input = 'U';
let result = /[aeiouAEIOU]/.test(input);
console.log(result);
Output
true
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.
Syntax:
string.indexOf(searchValue[, index])Example: In this example, we will check if check if a character is vowel or not in javascript using indexOf() method.
let input = 'Z';
let result =
'aeiouAEIOU'.indexOf(input) !== -1;
console.log(result);
Output
false
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.
Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example: In this example, we will check if check if a character is vowel or not in javascript using if-else statements.
let input = 'A';
if (
input === 'a' || input === 'A' ||
input === 'e' || input === 'E' ||
input === 'i' || input === 'I' ||
input === 'o' || input === 'O' ||
input === 'u' || input === 'U'
) {
console.log(true);
} else {
console.log(false);
}
Output
true
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`.
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
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.
Example:
function isVowel(char) {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
return char.length === 1 && vowels.has(char.toLowerCase());
}
console.log(isVowel('U'));
console.log(isVowel('p'));
console.log(isVowel('o'));
Output
true false true
Approach: Using Bitwise Operations
In this approach, we use bitwise operations to check if a character is a vowel. This method leverages the binary representation of the ASCII values of characters. By utilizing bitwise AND operations, we can efficiently determine if a character is one of the vowels.
Example: This example demonstrates the use of bitwise operations to check if a character is a vowel in JavaScript.
function isVowel(char) {
// Convert the character to lowercase
char = char.toLowerCase();
// Binary masks for vowels 'a', 'e', 'i', 'o', 'u'
const vowelMask = (1 << ('a'.charCodeAt(0) - 'a'.charCodeAt(0))) |
(1 << ('e'.charCodeAt(0) - 'a'.charCodeAt(0))) |
(1 << ('i'.charCodeAt(0) - 'a'.charCodeAt(0))) |
(1 << ('o'.charCodeAt(0) - 'a'.charCodeAt(0))) |
(1 << ('u'.charCodeAt(0) - 'a'.charCodeAt(0)));
// Calculate the binary mask for the input character
const charMask = 1 << (char.charCodeAt(0) - 'a'.charCodeAt(0));
// Check if the character's mask matches any vowel mask
return (charMask & vowelMask) !== 0;
}
// Test cases
console.log(isVowel('u')); // Output: true
console.log(isVowel('b')); // Output: false
console.log(isVowel('A')); // Output: true
console.log(isVowel('G')); // Output: false
Output
true false true false