JavaScript Program to Remove Vowels from a String
Last Updated :
14 May, 2024
The task is to write a JavaScript program that takes a string as input and returns the same string with all vowels removed. This means any occurrence of 'a', 'e', 'i', 'o', 'u' (both uppercase and lowercase) should be eliminated from the string.
Given a string, remove the vowels from the string and print the string without vowels.
Examples:
Input : welcome to geeksforgeeks
Output : wlcm t gksfrgks
Input : what is your name ?
Output : wht s yr nm ?
Using Regular Expressions
Regular expressions provide a concise and powerful way to manipulate strings in JavaScript. We can use the replace()
method along with a regular expression to remove vowels from a string.
In this approach, the regular expression [aeiouAEIOU]
matches any vowel (both lowercase and uppercase) in the string. The g
flag ensures that all occurrences of vowels are replaced.
Example: Implementation to remove vowels from a String using regular expression.
JavaScript
function removeVowels(str) {
return str.replace(/[aeiouAEIOU]/g, '');
}
let inputString = "Hey Geeks of GeeksforGeeks!";
let result = removeVowels(inputString);
console.log(result);
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(1)
Using a Loop
A simple loop can also be used to iterate over each character in the string and exclude vowels.
Example: Implementation to remove vowels from a String using iterative approach.
JavaScript
function removeVowels(str) {
let result = '';
for (let i = 0; i < str.length; i++) {
if (!'aeiouAEIOU'.includes(str[i])) {
result += str[i];
}
}
return result;
}
let inputString = "Hey Geeks of GeeksforGeeks!";
let result = removeVowels(inputString);
console.log(result);
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(1)
Using Array Methods
Another approach is to split the string into an array of characters, filter out the vowels, and then join the remaining characters back into a string.
Example: Implementation to remove vowels from a String using array methods.
JavaScript
function removeVowels(str) {
return str.split('').filter(
char => !'aeiouAEIOU'.includes(char)).join('');
}
let inputString = "Hey Geeks of GeeksforGeeks!";
let result = removeVowels(inputString);
console.log(result);
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n)
Similar Reads
JavaScript Program to Check if a Character is Vowel or Not 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 : TrueExplanation: 'u' is among the vowel characters family (a,e,i,o,u).Input : 'b'Output : Fal
4 min read
JavaScript Program to Reverse Consonants in a Given String Without Affecting the Other Elements In this article, we have given a string and the task is to write a javascript program to reverse only the consonants of a string without affecting the other elementsâ positions and printing the output in the console.Examples:Input: helloOutput: lelhoExplanation: h,l,l are consonants in the given str
6 min read
JavaScript - Find if a Character is a Vowel or Consonant Here are the different methods to find if a character is a vowel or consonant1. Using Conditional Statements (If-Else)The most widely used approach is to check the character against vowels (both uppercase and lowercase) using conditional statements (if-else). This method is simple and effective for
5 min read
Java Program to Count Number of Vowels in a String In java, the string is a sequence of characters and char is a single digit used to store variables. The char uses 2 bytes in java. In java, BufferedReader and InputStreamReader are used to read the input given by the user from the keyboard. Then readLine() is used for reading a line. The java.io pac
4 min read
JavaScript - How to Get a Number of Vowels in a String? Here are the various methods to get the number of vowels in a string using JavaScript.1. Using a for LoopThis is the most basic and beginner-friendly approach. It uses a loop to iterate over each character and checks if it is a vowel.JavaScriptconst cVowels = (s) => { const vowels = "aeiouAEIOU";
3 min read
R Program to Count the Number of Vowels in a String In this article, we will discuss how to Count the Number of Vowels in a String with its working example in the R Programming Language. It is a fundamental in programming that allows us to repeatedly execute a block of code as long as a specified condition remains true. It's often used for tasks like
5 min read