Vowel/Consonant Check

Program to check if a character is a vowel or consonant

BeginnerTopic: Conditional Programs
Back

JavaScript Vowel/Consonant Check Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Using if-else
function checkVowelConsonant(char) {
    // Convert to lowercase for case-insensitive check
    char = char.toLowerCase();
    
    if (char === 'a' || char === 'e' || char === 'i' || char === 'o' || char === 'u') {
        return "Vowel";
    } else if (char >= 'a' && char <= 'z') {
        return "Consonant";
    } else {
        return "Not a letter";
    }
}

console.log("a:", checkVowelConsonant('a'));
console.log("B:", checkVowelConsonant('B'));
console.log("z:", checkVowelConsonant('z'));
console.log("5:", checkVowelConsonant('5'));

// Method 2: Using array includes()
function checkVowelConsonant2(char) {
    char = char.toLowerCase();
    const vowels = ['a', 'e', 'i', 'o', 'u'];
    
    if (vowels.includes(char)) {
        return "Vowel";
    } else if (char >= 'a' && char <= 'z') {
        return "Consonant";
    } else {
        return "Not a letter";
    }
}

console.log("\nUsing includes():");
console.log("e:", checkVowelConsonant2('e'));
console.log("k:", checkVowelConsonant2('k'));

// Method 3: Using switch statement
function checkVowelConsonant3(char) {
    char = char.toLowerCase();
    
    switch(char) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return "Vowel";
        default:
            if (char >= 'a' && char <= 'z') {
                return "Consonant";
            } else {
                return "Not a letter";
            }
    }
}

console.log("\nUsing switch:");
console.log("i:", checkVowelConsonant3('i'));
console.log("m:", checkVowelConsonant3('m'));
Output
a: Vowel
B: Consonant
z: Consonant
5: Not a letter

Using includes():
e: Vowel
k: Consonant

Using switch:
i: Vowel
m: Consonant

Understanding Vowel/Consonant Check

This program demonstrates different ways to check if a character is a vowel or consonant.

Vowels and Consonants

-

Vowels

: a, e, i, o, u (and sometimes y)

-

Consonants

: All other letters

Method 1: If-Else with OR

Using logical OR (||) operator:

if (char === 'a' || char === 'e' || char === 'i' || char === 'o' || char === 'u') {
}
    return "Vowel";

String Methods

toLowerCase(): Converts to lowercase
toUpperCase(): Converts to uppercase
charAt(): Gets character at index

Method 2: Array includes()

Using array method (ES6):

const vowels = ['a', 'e', 'i', 'o', 'u'];
if (vowels.includes(char)) {
}
    return "Vowel";

Pros:

Cleaner code
Easy to modify vowels
More readable

Method 3: Switch Statement

Using switch for multiple cases:

switch(char) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    default:

        return "Consonant";
}
        return "Vowel";

Switch Statement

Checks value against multiple cases
break exits switch (not needed with return)
default handles unmatched cases

Character Validation

Check if character is a letter:

if (char >= 'a' && char <= 'z') {
}
    // It's a letter

When to Use:

-

If-else

: Simple conditions

-

includes()

: Modern, clean

-

Switch

: Multiple exact matches

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for Vowel/Consonant Check

This JavaScript program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents