Skip to content
OVEX TECH
Education & E-Learning

Understand Recursion: A Simple JavaScript Factorial Example

Understand Recursion: A Simple JavaScript Factorial Example

Learn How to Use Recursion in Coding

Recursion is a powerful idea in programming that helps solve complex problems by breaking them down into smaller, easier pieces. You’ll learn what recursion is and see a practical example using JavaScript to calculate factorials. This guide will make the concept clear and show you how to use it.

What is Recursion?

Imagine standing between two mirrors. You see your reflection repeated over and over, getting smaller each time.

Recursion in coding is similar: it’s when a function calls itself to solve a problem. It keeps calling itself until a specific condition is met, like the reflections getting too small to see.

Understanding Factorials

Before we dive into the code, let’s understand what a factorial is. A factorial is a way to multiply a whole number by all the positive whole numbers smaller than it.

For example, the factorial of 3 is calculated as 3 multiplied by 2, then multiplied by 1. That gives us 6.

The factorial of 5 is found by multiplying 5 by 4, then by 3, then by 2, and finally by 1. This calculation results in 120. You can see a pattern here: the multiplication continues until we reach 1.

The Base Case: The Stopping Point

This pattern of multiplying down to 1 is important. It’s what we call the ‘base case’ in recursion.

The base case is the condition that tells the function when to stop calling itself. Without a base case, the function would call itself forever, like an infinite reflection in the mirrors.

When the base case is true, the function stops repeating and gives back the final result. This is where the calculation is finished, and we don’t need to call the function again.

Writing a Recursive Factorial Function in JavaScript

Let’s look at how to write a recursive function in JavaScript to calculate factorials. This function will call itself until it reaches the base case.

  1. Define the Function

    First, we define a function, let’s call it `factorial`, that takes one number, `n`, as input. This `n` is the number for which we want to find the factorial.

  2. Check for the Base Case

    Inside the function, the first thing we do is check if `n` is equal to 1. This is our base case. If `n` is 1, the function should stop and return 1, because the factorial of 1 is just 1.

  3. The Recursive Step

    If `n` is not 1, the function needs to call itself with a smaller number. We do this by returning the result of `n` multiplied by the result of calling `factorial` with `n – 1`. This is the recursive step where the function calls itself.

    Think of it like this: to find the factorial of 5, we say it’s 5 times the factorial of 4. Then, to find the factorial of 4, we say it’s 4 times the factorial of 3, and so on. This continues until we need to find the factorial of 1.

  4. How it Works Together

    So, the function keeps calling itself with smaller numbers until `n` becomes 1. When `n` is 1, the base case is met, and the function returns 1.

    Then, the previous call gets that result (1) and multiplies it by its number (2), returning 2. This continues back up the chain until the original call finishes the calculation.

    For example, calculating factorial(3):

    • `factorial(3)` calls `factorial(2)` and waits.
    • `factorial(2)` calls `factorial(1)` and waits.
    • `factorial(1)` hits the base case and returns `1`.
    • `factorial(2)` receives `1`, calculates `2 * 1`, and returns `2`.
    • `factorial(3)` receives `2`, calculates `3 * 2`, and returns `6`.

Important: The Base Case is Crucial

Remember, the base case is absolutely essential. If you forget to include a base case or if it’s never met, your recursive function will run forever.

This can cause your program to crash or freeze because it runs out of memory. Always make sure your recursive function has a clear stopping point.

In Summary

Recursion is a technique where a function calls itself to solve a problem by breaking it into smaller, identical sub-problems. The key is to have a base case, which is the condition that stops the recursion. By understanding factorials and implementing a base case, you can effectively use recursion in your code.


Source: Recursion is a key concept in coding. Gavin explains it simply here. (YouTube)

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by

John Digweed

2,992 articles

Life-long learner.