JavaScript

# JavaScript Recursive Function

Recursion is a problem-solving approach in which you define a function that keeps invoking itself until it reaches the required outcome. Recursion is a good way to go when you need to call the same function multiple times with different parameters. It can be utilized in several situations; however, it excels at sorting, fractal math, and traversing non-linear data structures.

JavaScript Recursive functions are also straightforward to utilize because they are simple to construct, with a consistent and specific return value for the specified input, and do not affect external variables’ state.

This write-up will explain the working of JavaScript Recursive Function with the help of suitable examples. So, let’s start!

## JavaScript Recursive Function

A JavaScript “Recursive function” is a function that invokes itself, either directly or indirectly. With the help of recursion, a specific problem can be solved by returning the value call of the same function.

Also, at some point, the recursive function must be terminated. Internal conditions are frequently used to return a recursive function, which sends the logic down to a new iteration until the “base case” or base condition is satisfied.

Now, let’s understand what is a base case in the JavaScript recursive function.

## Base case in JavaScript Recursive Function

The base case of a recursive function is an iteration that does not require any further recursion to solve a problem. A JavaScript recursive function must have a base case; without it, a recursive function will never end, resulting in an infinite loop.

Syntax of JavaScript Recursive Function

function recurseFunc() {

//definition of recurseFunc()

recurseFunc();

}

recurseFunc();

Here, in the above-given syntax, the “recurseFunc()” is a JavaScript Recursive Function which invokes itself inside its body.

## Working of JavaScript Recursive Function

The goal of a JavaScript recursive function is to break down the main task into smaller segments or sub-tasks until a sub-task fails to meet the specified condition and does not enter into any other code block written within the recursive function.

In JavaScript, it is not essential to only use looping statements for implementing recursion; instead, conditional blocks such as the “if-else” statement can be utilized for the same purpose.

We will now check out some examples of implementing recursive functions in JavaScript.

## Example 1: Using JavaScript Recursive Function

In the following program, a recursive function is defined named “counter()”. This “counter()” function will count the number till “5”:

function counter(x) {

console.log(x);

const num= x + 1;

if (num< 6) {

counter(num);

}

}

The “counter()” function will call itself until the base case “num < 6” meets:

counter(1);

Execution of the above-given code will print out numbers from “1” to “5”: ## Example 2: Using JavaScript Recursive Function

The following program will recursively call the function “power()” for calculating the power of “2”, “4” times which will generate “16”.

Here, when the “power()” function is invoked, the execution block will divide into two parts based on the added conditional “if-else” statement. The “if” condition will check if the second number “y” equals “1”, and the “else” part refers to the operation of multiplying the first number “x” with the value returned by the “power(x, y – 1)” function:

function power(x,y) {

if (y == 1) {

return x;

}

else {

return x * power(x, y - 1);

}

}

console.log(( power(2, 4)));

As you can see from the output, we have successfully implemented the recursive function for calculating the “2” power “4,” which result in the value “16”: ## When to use JavaScript Recursive Function

Here is the list of some of the situations where you can use JavaScript Recursive Function:

• To resolve problems related to iterative branching such as binary search, traversal, sorting, or any other data structure, the usage of the recursive function is proved to be most effective and appropriate.
• JavaScript recursive functions are useful when it is required to call the same function multiple times while passing different arguments within a loop. For instance, you have to implement the Fibonacci series or calculate the factorial of a large number, then utilize the recursive function to solve the problem without any hassle.

## When to avoid JavaScript Recursive Function

Under the following circumstances, you should avoid using JavaScript Recursive Function:

• When an issue is too minor to be handled with just a few lines of basic code, one should avoid using Recursion to solve it. The reason is that the JavaScript recursive function will keep invoking itself till it meets the base case. As a result, the recursive function unnecessarily uses a significant amount of memory.
• It is also possible that if recursion is overused, the entire program will become infinite, and there will be no other option for its termination. So, you have to carefully use the recursion with correctness only where needed.

That was all essential information related to JavaScript Recursive Function. You can further explore it according to your preferences.

## Conclusion

A JavaScript Recursive function is a type of function that invokes itself, either directly or indirectly. With the help of recursion, a specific problem can be solved by returning the value call of the same function. A JavaScript recursive function must have a base case; without it, a recursive function will never end, resulting in an infinite loop. This write-up explained JavaScript Recursive Function with the help of suitable examples. 