# JavaScript Recursion

In this tutorial, you will learn about recursion in JavaScript with the assistance of examples.

## What Is Recursion?

Recursion is essentially when a function calls itself.

In the most essential of terms, recursion is the point at which a function continues to call itself until it doesn’t need to any longer.

What? yeah, the function continues to call itself yet with a smaller input every single time.

Consider recursion a circuit race. It resembles running a similar track again and again yet the laps continue to get smaller every time. Ultimately, you will run the last, littlest lap and the race will be finished.

Same with recursion: the function continues to call itself with smaller input and in the long run it stops.

Be that as it may, the function doesn’t choose for itself when to stop. We reveal to it when to stop. We give the function a condition known as a base case.

The base case is the condition that tells the function when to quit calling itself. It like mentioning to the function what the last lap in the race will be so it quits pursuing that lap.

Recursion is a process of calling itself. A function that calls itself is known as a recursive function.

The syntax for recursive function is:

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

recurse();```

Here, the recurse() function is a recursive function. It is calling itself inside the function.

A recursive function should have a condition to quit calling itself. Something else, the function is called inconclusively.

When the condition is met, the function quits calling itself. This is known as a base condition.

To prevent infinite recursion, you can use the if…else statement (or similar approach) where one branch settles on the recursive decision, and the other doesn’t.

Thus, it generally looks like this.

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

recurse();```

A simple example of a recursive function would be to count down the value to 1.

## Example 1: Print Numbers

```// 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```

In the above program, the user passes a number as an argument when calling a function.

In each iteration, the number worth is diminished by 1, and function countDown() is called until the number is positive. Here, newNumber > 0 is the base condition.

This recursive call can be clarified in the accompanying steps:

```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 reaches 0, the base condition is met, and the function is not called anymore.

## Example 2: Find Factorial

```// 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`

At the point when you call function factorial() with a positive number, it will recursively call itself by diminishing the number.

This process proceeds until the number becomes 1. At that point when the number arrives at 0, 1 is returned.

This recursive call can be explained in the following steps:

```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```

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.