JavaScript Program to Find if a Character is a Vowel or Consonant
Last Updated :
17 May, 2024
In this article, we will see different approaches to finding whether a character is a vowel or a consonant using JavaScript. We will check the condition of a character being Vowel and display the result.
Approaches to find if a character is a vowel or consonant
- Using conditional Statements
- Using JavaScript array and .include method
- Using Regular expression
In this method, we will use if-else conditional statements to check if the letter is a vowel or not and display the output result.
Example: This example demonstrates the conditional statements for vowels and consonants.
Javascript
function checkChar(char){
ch = char.toLowerCase();
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return console.log("Given character is a Vowel");
return console.log("Given character is a Consonent");
}
checkChar('G');
checkChar('A')
OutputGiven character is a Consonent
Given character is a Vowel
In this method, we will use JavaScript array of vowels and use includes method to check if given character is present in the array then it is a vowel and consonant otherwise.
Example: In this example, we will check if vowels array includes the input cahracter or not.
Javascript
function checkChar(char){
ch = char.toLowerCase();
const arr = ['a','e','i','o','u']
if(arr.includes(ch))
return console.log("Given character is a Vowel");
return console.log("Given character is a Consonent");
}
checkChar('E');
checkChar('J');
OutputGiven character is a Vowel
Given character is a Consonent
In this method, we will create a regular expression for the vowels and test if the given input char satisfy the regex condition.
Example: In this example, we will implement regex for the vowels and output the result.
Javascript
function checkChar(char){
ch = char.toLowerCase();
const regex = /^[aeiou]$/i;
if(regex.test(ch))
return console.log("Given character is a Vowel");
return console.log("Given character is a Consonent");
}
checkChar('I');
checkChar('Z');
OutputGiven character is a Vowel
Given character is a Consonent
Using Set
Using a Set, vowels are stored efficiently for quick lookup. If the character is in the set, it’s a vowel; otherwise, it’s a consonant if it’s a letter, or neither. This ensures O(1) time complexity for the check.
Example: The isVowelOrConsonant function checks if a character is a vowel, consonant, or neither using a Set for vowels and a regex for letters. It returns the appropriate classification.
JavaScript
function isVowelOrConsonant(char) {
const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
return vowels.has(char) ? 'Vowel' : (char.match(/[a-zA-Z]/) ? 'Consonant' : 'Neither');
}
console.log(isVowelOrConsonant('a')); // Output: "Vowel"
console.log(isVowelOrConsonant('b')); // Output: "Consonant"
console.log(isVowelOrConsonant('1')); // Output: "Neither"
OutputVowel
Consonant
Neither
Share your thoughts in the comments
Please Login to comment...