JavaScript Recursion

JavaScript Recursion with Example

With the help of examples, you will learn about recursion in JavaScript in this article. Recursion is the process by which anything calls itself. A recursive function is a function that calls itself.

The syntax for a recursive function is as follows:

function recurse() {
    // function code
    recurse();
    // function code
}

recurse();

The recurse() method is a recursive function in this case. It is invoking itself from within the function.

  • A recursive function must have a condition that causes it to stop calling itself. Otherwise, the function will be invoked indefinitely.
  • When the condition is met, the function no longer calls itself. This is known as a base condition.
  • To avoid infinite recursion, use an if…else statement (or a similar approach) in which one branch calls the recursive function and the other does not.

So, in general, it seems as follows:

function recurse() {
    if(condition) {
        recurse();
    }
    else {
        // stop calling recurse()
    }
}

recurse();

Example 1: Print Numbers Using Recursion

// program to count down numbers to 1
function countDown(number) {

    // display the number
    console.log(number);

    // decrease the number value
    const newNumber = number - 1;

    // base case
    if (newNumber > 0) {
        countDown(newNumber);
    }
}

countDown(4);

Output

4
3
2
1

  • When invoking a function in the preceding program, the user passes a number as an argument.
  • The number value is dropped by one in each iteration, and the method countDown() is called until the number is positive. The base condition is newNumber > 0.
  • The following steps will explain this recursive call:

countDown(4) prints 4 and calls countDown(3)
countDown(3) prints 3 and calls countDown(2)
countDown(2) prints 2 and calls countDown(1)
countDown(1) prints 1 and calls countDown(0)

When the number hits zero, the base condition is satisfied, and the function is no longer invoked.

Example 2: Find Factorial Using Recursion

// program to find the factorial of a number
function factorial(x) {

    // if number is 0
    if (x === 0) {
        return 1;
    }

    // if number is positive
    else {
        return x * factorial(x - 1);
    }
}

const num = 3;

// calling factorial() if num is non-negative
if (num > 0) {
    let result = factorial(num);
    console.log(`The factorial of ${num} is ${result}`);
}

Output

The factorial of 3 is 6

When you run factorial() with a positive integer, it will call itself recursively, reducing the value.

recursion javascript recursion javascript recursion javascript recursion javascript recursion javascript recursion javascript

This method is repeated until the number becomes one. When the number hits zero, the value 1 is returned.

The following steps will explain this recursive call:

recursion java recursion java recursion java recursion java recursion java recursion java

factorial(3) returns 3 * factorial(2)
factorial(2) returns 3 * 2 * factorial(1)
factorial(1) returns 3 * 2 * 1 * factorial(0)
factorial(0) returns 3 * 2 * 1 * 1

You may like:

Switch Statement in JavaScript

Leave a Reply