check vowels in javascript code example

Example 1: count vowels in javascript

const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];

function countVowels(sentence) {
  let counts = 0;
  for(let i = 0; i < vowels.length; i++) {
    if(vowels.includes(sentence[i])) {
      counts++;
    }
  }
  return console.log(counts);
}

countVowels('Hello World');
countVowels('AaEeIiOoUu');
countVowels('aaaaa');

Example 2: vowel check in javascript

// check the word is vowel or not
let words = "aeiou";

let newWords = "";

function isVowelOrNot(words) {
   for (let word of words) {
      if (
         word === "a" ||
         word === "e" ||
         word === "i" ||
         word === "o" ||
         word === "u"
      ) {
         newWords = newWords + word;
      }
   }
   if (newWords === words) {
      return true;
   } else {
      return false;
   }
}

let result = isVowelOrNot(words);
console.log(result);

Example 3: count vowels in a string javascript

// BEST and FASTER implementation using regex
const countVowels = (str) => (str.match(/[aeiou]/gi) || []).length