w3resource

JavaScript: Count the number of vowels in a given string

JavaScript Basic: Exercise-54 with Solution

Write a JavaScript program to count the number of vowels in a given string.

Pictorial Presentation:

JavaScript: Count the number of vowels of a given string

Sample Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JavaScript program to count the number of vowels in a given string.</title>
</head>
<body>

</body>
</html>

JavaScript Code:

function vowel_Count(str)
{ 

  return str.replace(/[^aeiou]/g, "").length; 
}

console.log(vowel_Count("Python"));
console.log(vowel_Count("w3resource.com"));

Sample Output:

1
5

Flowchart:

Flowchart: JavaScript - Count the number of vowels in a given string

ES6 Version:

function vowel_Count(str)
{ 

  return str.replace(/[^aeiou]/g, "").length; 
}

console.log(vowel_Count("Python"));
console.log(vowel_Count("w3resource.com"));

Live Demo:

See the Pen JavaScript - count the number of vowels in a given string - basic-ex-54 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Write a JavaScript program to check if the characters a and b are separated by exactly 3 places anywhere (at least once) in a given string.
Next: Write a JavaScript program to check if a given string contains equal number of p's and t's present.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

JavaScript: Tips of the Day

Assigns default values for all properties in an object that are undefined

Example:

const tips_defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);
console.log(tips_defaults({ p: 1 }, { q: 2 }, { q: 6 }, { p: 3 })); 

Output:

[object Object] {
  p: 1,
  q: 2
}

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook