JavaScript Recursion in Real LifeWe 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 reallife 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 FunctionRecursion 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 RecursionRecursion 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 RecursionOne 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 RealLife Examples of RecursionGenerally, Recursion is the favorite topic for the interviewers as they often interrogate for recursion questions. There are various examples of our reallife 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 NowOutput: 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 timeconsuming. 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 NowOutput: In the above code,
Example 3: Puzzle Solving It is the best option to choose Recursion for solving puzzles because Recursion can help to get the bestoptimized solution for the puzzle. There are many puzzles we may play in our daytoday life, such as tictactoe. 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 tictactoe 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: 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:
For example: Climbing a ladder
Similarly, there are more examples of inductive proofs that can be solved using Recursion. Thus, all these are the realworld examples of Recursion and the examples where we use the Recursion.
Next TopicJavaScript removeChild
