How to Extract Vowels from a String in JavaScript?

Estimated read time 1 min read

You can write a custom function in JavaScript to extract vowels from a string. Here is an example of how you can write a function that takes a string as input and returns its vowels:

function extractVowels(str) {
  let vowels = "aeiouAEIOU";
  let result = "";
  for (let i = 0; i < str.length; i++) {
    if (vowels.indexOf(str[i]) !== -1) {
      result += str[i];
    }
  }
  return result;
}

console.log(extractVowels("Hello World!"));
// Output: "eo o"

In the example, the function uses a loop to iterate over each character in the input string. If a character is a vowel (as determined by checking if it exists in the vowels string), it is added to the result string. The final result is returned as the output of the function.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply