We use the Recursion function when we want to write a chunk of code once but use it multiple times. You can call the functions as many times as you want in your program.
In this article, we have provided a quick guide to recursion in JavaScript.
Let’s jump to it without further ado.
What is Recursion?
Recursion is a process in programming that is useful to simplify any task and performs it by splitting the task into several sections by calling it several times.
The function that does this job is called a recursive function. The function calls itself until it reaches the desired output. However, if you do not stop the function, it will keep running forever. A recursive function is more spontaneous than loops.
The syntax for the recursive function is given below:
Here, recurse() is calling itself inside the recurse() function. The function must have a certain condition to stop calling it infinitely.
What is the Base Case, and When Do We Use It?
In the above syntax, we did not use the base case condition.
If the condition is not given, the recursive function will call itself infinitely. As a result, the function will halt the system and crash your browser. That’s when the base case comes in handy, and it is an essential step in recursion.
The base case is a condition use inside the recursive function to stop it from calling itself until a specific condition is met.
In this case, we have used if-statement to stop the function when x reaches a specific value.
Let’s Guide You With Some Recursion Example
Let’s look at some of the recursive function with example
Example:1– Suppose you want to count down from a specific number to zero.
The first thing we did was define the base so that the function could know when to stop. Otherwise, stack overflow is going to happen.
In this program, while calling the function countDownFrom, the user gave a number as an argument. The number is decreased by one in each iteration.
And the function is calling itself until the number goes down to zero.
The function stopped when it reached the base condition.
Example:2-
Write a JavaScript recursive function named pow(x,n), meaning that the number x raises to a natural power of n.
Recursion VS Loops
The above two examples can be done with loops as well. Here are the reasons:
- The recursive function has a minimum length of code and elegant to use as the function call itself until a specific condition is met. But the time complexity of recursion can get higher due to the considerable number of the recursive call.
However, iteration repeats over a block of code, thus involve a larger size of codes. The time complexity is less than recursion.
- Infinite repetition can lead the recursive function to crash the computer system. So you have to be careful while giving conditions to the base case.
However, infinite iteration stops the execution any farther.
- Recursion has a myriad of overhead as compared to loops. Thus, it is slower than iteration.
There are many downsides to using recursive function. You might think, then why should you use it?
The answer lies in the ease of its usage. You can explore data structures like trees; for example, you can find all the DOM element and their class name with recursion.
Whenever you find something stressful or challenging to find a solution using iteration, we recommend using recursion instead.
Recursion is a robust technique in many cases. It is the easiest way to solve a complex algorithm or finding non-linear data structures.
At first, it will be tough for you to decide which one you should use. Iteration or recursion? We recommend using both when solving a problem. And by the time you will get an idea when to use recursion. Now, it’s your call!
0 responses on "A Quick Guide to Recursion in JavaScript"