how to find the number of vowels in a string 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: find vowels in string javascript

var name = userInput[0];
    var count =0;
  	function findvowel(name)
    {
    	var vowel = ['a','e','i','o','u','A','E','I','O','U'];
      	for(i=0;i<name.length;i++)
      	{
      		if(vowel.includes(name[i]))
        	{
          		count++;
        	}
      	}
      	return count;
    }
    
  console.log(findvowel(name));