Open In App

JavaScript Program to Check if a Character is Vowel or Not

Last Updated : 06 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Approach 1: 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);


Output

true

Approach 2: 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);


Output

true

Approach 3: 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);


Output

false

Approach 4: 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);
}


Output

true


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads