Horje
JavaScript Program to Remove Vowels from a String

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

Output
Hy Gks f GksfrGks!

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

Output
Hy Gks f GksfrGks!

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

Output
Hy Gks f GksfrGks!

Time Complexity: O(n), where n is the length of the string

Auxiliary Space: O(n)




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program for Nth Catlan Numbers JavaScript Program for Nth Catlan Numbers
How to Update an Array of Objects with Another Array of Objects using Lodash? How to Update an Array of Objects with Another Array of Objects using Lodash?
JavaScript Best Practices for Code Review JavaScript Best Practices for Code Review
Check if the ith Bit is Set or Not using JavaScript Check if the ith Bit is Set or Not using JavaScript
Replace all &#039;0&#039; with &#039;5&#039; in an Input Integer using JavaScript Replace all &#039;0&#039; with &#039;5&#039; in an Input Integer using JavaScript

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