JavaScript Program to Check if a Character is Vowel or Not
Last Updated :
09 Jul, 2024
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).
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.
JavaScript
let input = 'a';
let vowels =
['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
let result = vowels.includes(input);
console.log(result);
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.
JavaScript
let input = 'U';
let result = /[aeiouAEIOU]/.test(input);
console.log(result);
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.
JavaScript
let input = 'Z';
let result =
'aeiouAEIOU'.indexOf(input) !== -1;
console.log(result);
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.
JavaScript
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);
}
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:
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
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:
JavaScript
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'));
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.
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
Outputtrue
false
true
false
Similar Reads
JavaScript - Find if a Character is a Vowel or Consonant
Here are the different methods to find if a character is a vowel or consonant1. Using Conditional Statements (If-Else)The most widely used approach is to check the character against vowels (both uppercase and lowercase) using conditional statements (if-else). This method is simple and effective for
5 min read
JavaScript Program to Find Missing Characters to Make a String Pangram
We have given an input string and we need to find all the characters that are missing from the input string. We have to print all the output in the alphabetic order using JavaScript language. Below we have added the examples for better understanding. Examples: Input : welcome to geeksforgeeksOutput
6 min read
JavaScript Program to Check if a Given String is a Rotation of a Palindrome
In this article, we will see how to check a given string is a rotation of a palindrome. A palindrome is a string that remains the same when its characters are reversed (e.g., "racecar" is a palindrome).Example:Input: str = "racecar"Output: true // "racecar" is a rotation of a palindrome "racecar"Inp
6 min read
JavaScript - String Contains Only Alphabetic Characters or Not
Here are several methods to check if a string contains only alphabetic characters in JavaScriptUsing Regular Expression (/^[A-Za-z]+$/) - Most USedThe most common approach is to use a regular expression to match only alphabetic characters (both uppercase and lowercase).JavaScriptlet s = "HelloWorld"
2 min read
JavaScript Program to Remove Vowels from a String
The task is to write a JavaScript program that takes a string as input and returns the same string with all vowels removed. This means any occurrence of 'a', 'e', 'i', 'o', 'u' (both uppercase and lowercase) should be eliminated from the string. Given a string, remove the vowels from the string and
2 min read
JavaScript Program to Match Single Character with Multiple Possibilities
In this article, we will write programs to check all possible matching in JavaScript. We will check the occurrence of characters individually in the given string or sequence. In this article, we will check the occurrence of vowels in the given string. Table of Content Using JavaScript RegExp test()
3 min read
JavaScript Program to Validate String for Uppercase, Lowercase, Special Characters, and Numbers
In this article, we are going to learn how can we check if a string contains uppercase, lowercase, special characters, and numeric values. We have given string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and nu
4 min read
JavaScript Program to Check for Palindrome String using Recursion
Given a string, write a recursive function that checks if the given string is a palindrome, else, not a palindrome. A string is called a palindrome if the reverse of the string is the same as the original one. For example - âmadamâ, âracecarâ, etc.What is Recursion?The process in which a function ca
3 min read
JavaScript Program to Reverse Consonants in a Given String Without Affecting the Other Elements
In this article, we have given a string and the task is to write a javascript program to reverse only the consonants of a string without affecting the other elementsâ positions and printing the output in the console.Examples:Input: helloOutput: lelhoExplanation: h,l,l are consonants in the given str
6 min read
JavaScript Program to find Lexicographically next String
In this article, we are going to learn how can we find the Lexicographically next string. Lexicographically next string refers to finding the string that follows a given string in a dictionary or alphabetical order.Examples: Input : testOutput : tesuExplanation : The last character 't' is changed to
3 min read