Javatpoint Logo
Javatpoint Logo

JavaScript Recursion in Real Life

We would have heard and learned the recursion concept when approaching different programming languages. In JavaScript also, we have the concept of Recursion, where we make use of the recursive functions.

So, in this section, we are going to learn about Recursion and will also see some real-life examples of Recursion. We will also discuss that when we should use Recursion and when we should avoid its use.

What is Recursion and Recursive Function

Recursion is just the recursive call of a function to itself, where a function calls itself recursively. Such a type of function is called recursive function, and the approach is known as Recursion. Through Recursion, we can solve many complex problems with elegant solutions. However, it is recommended that we should avoid making use of Recursion because if we misuse it, it can be dangerous for the system and the data stored in it. Also, the JavaScript functional coding style does not support it, and some compilers are not able to handle recursive functions safely.

The syntax for a recursive function is given below:

One should note that a recursive function must have a condition so that it should stop its execution; otherwise, the function will be invoked and executed for infinite times.

When to use Recursion

Recursion is capable of breaking down large and complex problems into small problems. However, not every complex problem can be solved via Recursion. The use of the recursive function is most appropriate and effective for problems like iterative branching, such as sorting, traversing, binary search, and other data structures. Also, it is good to use recursive functions when there is a need to invoke the same function again and again but with different parameter values within a loop. For example, finding factorial of a number (a large number) using Recursion, Fibonacci series implementation, Tower of Hanoi are the best and easy to solve through Recursion.

When to avoid using Recursion

One should avoid solving a problem with Recursion in case the problem is too small and can be solved with just a few lines of simple code. It is because recursive functions invoke itself unless it is not stopped. Thus it uses a good amount of memory unnecessarily. Therefore, when we are solving a problem that can be solved without the use of Recursion, avoid using it. Sometimes, there may be a case that if we misuse Recursion, the whole program may get infinite, and there would be no better option than terminating the program. So, use Recursion wherever needed and with correctness.

Implementing Real-Life Examples of Recursion

Generally, Recursion is the favorite topic for the interviewers as they often interrogate for recursion questions. There are various examples of our real-life where Recursion is used:

Example1: Search Algorithms

There are many search algorithms we have learned where recursion is used such as in binary search. So, below code shows the use of recursion in binary search:

Test it Now


JavaScript Recursion in Real Life

There is an alternative method also available for solving binary search without Recursion but traversing and iterating the whole array for searching the element is complex and time-consuming. Thus, it becomes simple to solve it using Recursion.

Example 2: Delay Timers

Suppose after a set period of time we want to execute a function for multiple times, it is the best option to make use of recursion. So, let's see the code implementation for the same:

Test it Now


JavaScript Recursion in Real Life

In the above code,

  • We have set a delay timer due to which the elements present in the array will be printed onto the screen after a second, as we can see in the output snapshot.
  • Also, the same process will go on until the last value is not reached.
  • You can also notice that we have used Recursion here, and the function has invoked itself until the if condition became false.

Example 3: Puzzle Solving

It is the best option to choose Recursion for solving puzzles because Recursion can help to get the best-optimized solution for the puzzle. There are many puzzles we may play in our day-to-day life, such as tic-tac-toe. One must know how typical it would be if we try to solve with an iterative solution because for every move, we need to make several consideration factors - a complex task. However, it becomes easy or less complex to find the best move via a recursive algorithm. Thus, not only for tic-tac-toe but also in those games where you know that there are multiple iterative moves and need to determine the best move, Recursion is the best way for it.

Example 4: Fractal Designs

For solving the fractal designs, we can make use of Recursion. Fractal designs/patterns are the designs that are defined recursively. These fractal designs look as:

JavaScript Recursion in Real Life

The pattern consists of several complex steps which are difficult to solve. But when we try to solve the fractal design with the recursion method, we can judge and see the output of each particular step.

Example 5: Inductive Proofs

An inductive proof is a composition of base case and inductive steps where:

  • The base case is the case that can be demonstrated true for a particular value (P(0)).
  • The inductive step tells that we can get P(n) to P(n+1).

For example: Climbing a ladder

JavaScript Recursion in Real Life
  • The base case in this example is that 'Can we climb on the first rung of the ladder?'
  • The inductive step here is 'Can we get from one rung to the next with an aim of some arbitrary rung on a ladder?' The answer to this is that yes, we can get from one rung to the next rung of the ladder when assuming the ladder to be a perfect and infinitely long ladder. However, there are real-world limitations existing over the example. It is because the ladder might not be necessarily perfect, i.e., it cannot be infinitely tall, and also, the rungs might be missing or not evenly spaced. But we assume it as a perfect ladder. So, with these two components, it has been proved that we can climb from the ground to any rung on the ladder.

Similarly, there are more examples of inductive proofs that can be solved using Recursion.

Thus, all these are the real-world examples of Recursion and the examples where we use the Recursion.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA