paint-brush
An Introduction to Recursion in JavaScriptby@afrocloud

An Introduction to Recursion in JavaScript

by CloudMarch 28th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

An introduction to recursion in JavaScript.
featured image - An Introduction to Recursion in JavaScript
Cloud HackerNoon profile picture

Welcome, JavaScript folks! Today, we delve into the interesting world of recursion, a programming concept that can both beguile and empower coders. We'll take a look at what recursion is, talk about the building blocks of recursion and common pitfalls, and explore when recursion shines (and when it might not).

1. What is Recursion?

Recursion, in essence, is a function's ability to call itself. It creates a self-referential loop where the function breaks down a problem into smaller, similar subproblems, eventually reaching a base case that halts the recursion and provides the final answer. Imagine a set of Russian nesting dolls: the largest doll contains a smaller doll, which in turn contains an even smaller one, and so on until you reach the tiniest doll. Recursion works similarly, with each function call representing a smaller version of the original problem.

2. Building Blocks of a Recursive Algorithm

Here are the building blocks to build a recursive algorithm:

  • Base Case: This is the all-important exit strategy. It's a simple condition that terminates the recursion and provides the solution for the smallest possible subproblem. Without a base case, your function would call itself infinitely, leading to a stack overflow error (we'll get to that later).
  • Recursive Case: This is where the function breaks down the larger problem into a smaller, similar one. It typically involves modifying the input parameter and making a recursive call with the modified value.
  • Return Statement: In each function call, a return statement is crucial. It sends the result back up the call stack, eventually leading to the final output.

Let's dive into some code examples to solidify these concepts.

3a. Example #1: Summing a List of Numbers with Recursion

We'll start with the example of summing a list of numbers using recursion.

function sumRecursive(list) {
if (list.length === 0) {
return 0; // Base case: empty list sum is 0
} else {
const firstElement = list[0];
const remainingList = list.slice(1);
return firstElement + sumRecursive(remainingList); // Recursive case
}
}

const numbers = [1, 2, 3, 4, 5];
const sum = sumRecursive(numbers);
console.log(sum); // Output: 15
  • The sumRecursive function takes a list of numbers as input.
  • The base case checks if the list is empty. If so, it returns 0 (the sum of an empty list).
  • In the recursive case, the function extracts the first element from the list and stores the remaining elements in a new list.
  • It then returns the sum of the first element and the result of recursively calling sumRecursive with the remaining list. This process continues until the base case is reached.

3b. Summing a List of Numbers with Iteration (For Comparison)

Here's how we might achieve the same result using a for loop (iteration):

function sumIterative(list) {
let sum = 0;
for (const num of list) {
sum += num;
}
return sum;
}

const numbers = [1, 2, 3, 4, 5];
const sum = sumIterative(numbers);
console.log(sum); // Output: 15
  • The sumRecursive function takes a list of numbers as input.
  • We declare a variable sum to keep count of the total sum of the array.
  • We then iterate over the list of numbers and, at each iteration, add the current number to the value of sum.
  • Lastly, we return the value of sum.

This iterative approach is generally considered more efficient for simple tasks like summation. However, recursion shines in solving problems with a naturally recursive structure, as we'll see in the next example.

4a. Example #2: Fibonacci Sequence with Recursion

The Fibonacci sequence is a famous mathematical series where each number is the sum of the two preceding numbers (except the first two numbers, which are typically defined as 0 and 1). Let's see how recursion tackles this problem:

function fibonacciRecursive(n) {
if (n <= 1) {
return n; // Base case: 0 or 1
} else {
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2); // Recursive case
}
}

const number = 6;
const result = fibonacciRecursive(number);
console.log(result); // Output: 8
  • The fibonacciRecursive function takes an index n as input, representing the position in the Fibonacci sequence.
  • The base case checks if n is less than or equal to one, which accounts for 0 being 0 and 1 being 1 in the Fibonacci sequence
  • If we aren't in the base case, we go off of the definition of the Fibonacci sequence by calculating the intermediate sequence of n - 1 added to n - 2

4b. Example #2: Fibonacci Sequence with Iteration (For Comparison)

function fibonacciIterative(n) {
// Handle negative or zero input
if (n <= 0) {
return 0;
}

// Initialize first two Fibonacci numbers
let a = 0;
let b = 1;

// Iterate n-1 times (since the first two numbers are already defined)
for (let i = 2; i <= n; i++) {
let c = a + b;
a = b;
b = c;
}

// Return the nth Fibonacci number
return b;
}

// Example usage
console.log(fibonacciIterative(6)); // Output: 8

Alright, we've reached the halfway point in our exploration of recursion in JavaScript. If your mind is a little blown right now, that's perfectly normal! Recursion can be a head-scratcher at first, but with practice, it becomes a powerful tool.


In the previous section, we saw how recursion can be used to solve problems like summing a list of numbers and generating the Fibonacci sequence. However, it's crucial to understand the potential pitfalls and trade-offs when using recursion compared to iterative approaches.

5. Common Pitfalls of Recursive Programs

  • Stack Overflow: Recursive calls create a stack frame for each function invocation. If the base case isn't reached, these stack frames can keep growing, eventually leading to a stack overflow error. This can happen if the recursion goes too deep or if there's a bug in the base case condition.
  • Performance: While recursion can be elegant, it often comes at a performance cost. Each recursive call involves function call overhead, and for certain problems, iterative solutions can be significantly faster.
  • Readability: For complex problems, recursive code can become convoluted and challenging to understand. If the logic gets too nested, it might be better to refactor using iteration for improved readability.

Here's an example of a potential stack overflow with recursion:

function wrongFactorial(n) {
if (n === 0) {
return 1;
} else {
return n * wrongFactorial(n); // We never change the value of n
}
}

wrongFactorial(5); // This will result in a stack overflow error

This function attempts to calculate the factorial of a number (3! = 3 * 2 * 1). However, it lacks a clause to change the value of n. This would cause the function to call itself infinitely, leading to a stack overflow error.

6. Recursion vs Iteration

So, when should you choose recursion over iteration? Here are some ideas on that:

  • Recursion is a good choice for problems that can be naturally broken down into smaller, self-similar subproblems. The Fibonacci sequence is a classic example.
  • Recursion can sometimes lead to more concise and readable code for specific problems. It can promote a more declarative style, focusing on "what" needs to be done rather than the nitty-gritty "how."
  • However, for performance-critical tasks or problems that involve large datasets, iteration is often preferred. It typically has a smaller memory footprint and can be more efficient.

Ultimately, the choice between recursion and iteration depends on the specific problem you're trying to solve. Consider the trade-offs in terms of readability, performance, and potential for stack overflows.

Hopefully this was a helpful introduction or refresher on recursion! Definitely practice problems on platforms like Leetcode to solidify your understanding! Cheers!