Horje
JavaScript Program to Find if a Character is a Vowel or Consonant

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

1. Using conditional statements

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')

Output
Given character is a Consonent
Given character is a Vowel

2. Using JavaScript array and Array .includes() method

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');

Output
Given character is a Vowel
Given character is a Consonent

3. Using JavaScript regular expression

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');

Output
Given character is a Vowel
Given character is a Consonent

4. 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"

Output
Vowel
Consonant
Neither

5. Using Switch Statement

In this approach, we can utilize a switch statement to check if the character is a vowel or a consonant. We can define cases for each vowel and a default case for consonants.

Example:

JavaScript
function checkChar(char) {
    let ch = char.toLowerCase();
    switch(ch) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            console.log("Given character is a Vowel");
            break;
        default:
            console.log("Given character is a Consonant");
    }
}

checkChar('G'); // Output: Given character is a Consonant
checkChar('A'); // Output: Given character is a Vowel

Output
Given character is a Consonant
Given character is a Vowel

Using Character Codes

Using character codes to check if a character is a vowel involves converting the character to lowercase and comparing it against the specific vowel characters. This approach uses simple conditional checks to determine if the character is one of the vowels.

Example:

JavaScript
const isVowelOrConsonant = (char) => {
    const lowerChar = char.toLowerCase();
    return (lowerChar.charCodeAt(0) === 97 || lowerChar.charCodeAt(0) === 101 ||
        lowerChar.charCodeAt(0) === 105 || lowerChar.charCodeAt(0) === 111 ||
        lowerChar.charCodeAt(0) === 117) ? 'Vowel' : 'Consonant';
};

console.log(isVowelOrConsonant('a')); // Vowel
console.log(isVowelOrConsonant('b')); // Consonant

Output
Vowel
Consonant





Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Update the Value for a Specific Key in Map JavaScript Program to Update the Value for a Specific Key in Map
JavaScript Program to Convert Date to String JavaScript Program to Convert Date to String
JavaScript Program to Print Square Star Pattern JavaScript Program to Print Square Star Pattern
JavaScript Program to Check if a Given Integer is Positive, Negative, or Zero JavaScript Program to Check if a Given Integer is Positive, Negative, or Zero
JavaScript Program for Hollow Pyramid Star Pattern JavaScript Program for Hollow Pyramid Star Pattern

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
10