TLDR - Recursion in JavaScript

September 08, 2019

What is Recursion

Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem.

Problem: Counting Vowels of a String

Not using Recursion

function isVowel(char) {
  return ["a","e","i","o","u"].includes(char);
}

function countVowels(str) {
  let count = 0;
  for (let i = 0; i < str.length; i++) {
    if (isVowel(str[i])) {
      count++;
    }
  }
  return count;
}

Using Recursion

function countVowels(str) {
  if (str.length == 0) return 0;
  let first = (isVowel(str[0])) ? 1 : 0;
  return first + countVowels(str.slice(1));
}