FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Count Vowels

Count Vowels

easy
By FrontendAtlas Editorial · Updated Jan 30, 2026
Implement countVowels(str) that returns how many vowels appear in a string. Normalize case, decide whether to include 'y', and handle empty input safely. Use a Set for O(1) membership checks and a single O(n) pass. Concepts: strings, iteration, counting, sets.

Arguments

  • str: string — The input string to analyze.

Returns

number — The number of vowels found in the string.
Examples
// Basic examples
countVowels('hello') // => 2

countVowels('rhythm') // => 0

countVowels('AEIOU') // => 5

// Mixed case and spaces
countVowels('A quick brown fox') // => 5

Solution

Overview

Scan the string once, normalize case, and count only characters that belong to a vowel set. This keeps the solution O(n), handles empty input gracefully, and avoids tricky regex edge cases. It’s a classic example of membership checks with a Set. Key concepts: strings, iteration, counting.

1

Approach 1: Iterative counting (explicit and clear)

Steps:

1) Create a Set of vowels (e.g., a/e/i/o/u).

2) Convert the input to lowercase to make matching case-insensitive.

3) Initialize a counter at 0 and loop through each character.

4) If the character is in the vowel Set, increment the counter.

5) Return the counter (0 for empty strings).

Complexity: O(n) time, O(1) space for the fixed vowel set.

export default function countVowels(str) {
  const vowels = 'aeiou';
  let count = 0;
  for (const ch of str.toLowerCase()) {
    if (vowels.includes(ch)) count++;
  }
  return count;
}
export default function countVowels(str: string): number {
  const vowels = 'aeiou';
  let count = 0;
  for (const ch of str.toLowerCase()) {
    if (vowels.includes(ch)) count++;
  }
  return count;
}
2

Approach 2: Regular expression (concise alternative)

Idea: use a regular expression to directly match all vowels.

We can write: (str.match(/[aeiou]/gi) || []).length.

Read it as: *find all vowels (case-insensitive) and count them*. The /g flag finds all matches, and /i makes it case-insensitive.

Why use it: shorter and declarative, though less explicit than manual iteration. Good as a follow-up solution when you're comfortable with regex.

export default function countVowels(str) {
  return (str.match(/[aeiou]/gi) || []).length;
}
export default function countVowels(str: string): number {
  return (str.match(/[aeiou]/gi) || []).length;
}

Notes & Pitfalls

Pitfalls
  • Empty strings should return `0`.
  • Regex approach may be less intuitive for beginners.
  • Non-letter characters (spaces, punctuation) are simply skipped.
  • `y` is not treated as a vowel here.
Edge cases
  • No vowels → returns `0`.
  • Mixed case → handled by `toLowerCase()` or the `/i` flag.
  • Accented vowels (é, ü) → not counted unless explicitly included in the vowel set or regex.
Techniques
  • String traversal and membership checking.
  • Regex matching and counting via array length.

Resources

  • MDN – String.prototype.includes()
  • MDN – String.prototype.match()

Similar questions

Remove DuplicateseasyFind Maximum Occurring CharactereasyReverse a Stringeasy

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
← Prev←75 / 88