Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Vowel/Consonant Check in JavaScript

Check if a letter is a vowel or consonant in JavaScript with if-else, includes() and switch. Covers case, non-letters and Y. Explanation and output.

BeginnerJavaScript Conditional ProgramsExample 3 of 5
vowel-consonant-check.js
1// Method 1: Using if-else
2function checkVowelConsonant(char) {
3 // Convert to lowercase for case-insensitive check
4 char = char.toLowerCase();
5
6 if (char === 'a' || char === 'e' || char === 'i' || char === 'o' || char === 'u') {
7 return "Vowel";
8 } else if (char >= 'a' && char <= 'z') {
9 return "Consonant";
10 } else {
11 return "Not a letter";
12 }
13}
14
15console.log("a:", checkVowelConsonant('a'));
16console.log("B:", checkVowelConsonant('B'));
17console.log("z:", checkVowelConsonant('z'));
18console.log("5:", checkVowelConsonant('5'));
19
20// Method 2: Using array includes()
21function checkVowelConsonant2(char) {
22 char = char.toLowerCase();
23 const vowels = ['a', 'e', 'i', 'o', 'u'];
24
25 if (vowels.includes(char)) {
26 return "Vowel";
27 } else if (char >= 'a' && char <= 'z') {
28 return "Consonant";
29 } else {
30 return "Not a letter";
31 }
32}
33
34console.log("\nUsing includes():");
35console.log("e:", checkVowelConsonant2('e'));
36console.log("k:", checkVowelConsonant2('k'));
37
38// Method 3: Using switch statement
39function checkVowelConsonant3(char) {
40 char = char.toLowerCase();
41
42 switch(char) {
43 case 'a':
44 case 'e':
45 case 'i':
46 case 'o':
47 case 'u':
48 return "Vowel";
49 default:
50 if (char >= 'a' && char <= 'z') {
51 return "Consonant";
52 } else {
53 return "Not a letter";
54 }
55 }
56}
57
58console.log("\nUsing switch:");
59console.log("i:", checkVowelConsonant3('i'));
60console.log("m:", checkVowelConsonant3('m'));

Sample Output

a: Vowel
B: Consonant
z: Consonant
5: Not a letter

Using includes():
e: Vowel
k: Consonant

Using switch:
i: Vowel
m: Consonant

In this one

  • Why you lowercase input before comparing
  • How || joins several conditions
  • How includes() checks membership
  • How switch fall-through groups cases

How it works

  1. 1

    'a' matches a vowel and returns Vowel.

  2. 2

    'B' becomes 'b', is between a and z, so Consonant.

  3. 3

    '5' fails both checks and returns Not a letter.

  4. 4

    The includes() and switch versions give the same answers for e, k, i and m.

Edge cases

Capital letters

Without toLowerCase, 'E' is not found in the vowel list.

Always normalise case first so A and a behave the same.

More than one character

checkVowelConsonant('ae') returns Consonant.

The string "ae" is not equal to any single vowel, but it still sorts between "a" and "z". Check char.length === 1 before testing.

Letters from other languages

'é' is not in a to z.

This program only handles plain English letters. It returns Not a letter for é.

What's going on

This program looks at one character and says whether it is a vowel, a consonant, or not a letter at all. Vowels in English are a, e, i, o and u. Every other letter from a to z is a consonant.

The first step in every method is char.toLowerCase(). This turns "B" into "b". Without it, capital letters would not match the lowercase vowel list. "A" would be called a consonant, which is wrong.

Method 1 uses if-else with ||, which means "or". If the letter equals any of the five vowels, it returns "Vowel". Next it checks char >= 'a' && char <= 'z'. Strings compare by character code, and the letters a to z sit in order, so this test means "is it a lowercase letter". If it passes, the letter is a consonant. Anything else, like "5", is "Not a letter".

Method 2 keeps the vowels in an array and uses includes(), which returns true when the array has that value. This is shorter and easier to change.

Method 3 uses switch. Several case lines stacked together share one return. That is called fall-through. The default branch handles every other character.

In this program Y always counts as a consonant. Linguists can argue about words like "gym", but the code keeps one simple rule.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does 'A'.toLowerCase() return?

Pick an answer to check it.

Question 2

What does ['a','e'].includes('e') return?

Pick an answer to check it.

Questions people ask

Why is Y a consonant here?

The program uses the simple five-vowel rule so the logic stays easy to follow.

Can I use a string instead of an array?

Yes. 'aeiou'.includes(char) works the same way for single letters.

Why does switch have no break here?

Each case returns, and return leaves the function, so break is not needed.