💫Recursion in JavaScript💫
Introduction to Recursion in JavaScript: How It Works and How to Use It
In this tutorial, We will learn what recursion is😉, how recursion in JavaScript works and also how to implement it.
so lets get started 😄...
Recursion is essentially when a function calls itself.
What is Recursion?🤔
The simplest way to describe what recursion is by saying that it is a function that calls itself
. This type of function is called a “recursive function”
. It doesn’t matter if it is recursion in JavaScript or any other language. The main idea is that you have a function and this function calls itself, at least once.
ohhh boi that was so easy no🤭
// Simple recursive function
function recursiveFunction() {
// Call the recursive function again
recursiveFunction()
}
// Call the recursiveFunction()
recursiveFunction()
Let’s say you have a function. This function calls itself. What happens when you call this function?🤔
Well, it will
call itself
.
What happens next? 🤷♀️ When that function calls itself it will call itself again, and again and again
. The problem is that there is no point at which the function is terminated. The result is an infinite loop
😵💫.
For example,this will happen if you try to run the function in the example above. When you run that function, you will get the error Uncaught RangeError: Maximum call stack size exceeded
. You can avoid this problem, by creating an infinite loop
, by adding a base case
to the recursive function.
Base case 💥
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.
The easiest way to create this base case is by using a simple if…else statement
. Inside one block, either if or else depending on the condition, you will return
some value. Inside the other block, you will call the recursive function again
. This will allow you to terminate the function at the right time.😌
// Simple recursive function
function recursiveFunction() {
// Add base case
if (/* condition */) {
// Call the recursive function again
recursiveFunction()
} else {
// Return something instead of calling
// the recursive function again
}
}
// Call the recursive function
recursiveFunction()
Working 🏗️
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.
Let's learn with an example:
Example : 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 the 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.🤗
When to use 🧐
Where you can use JavaScript Recursive Function
:
- JavaScript recursive functions are useful when it is required to call the same function multiple times while passing different arguments within a loop.
When to avoid 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.
Thanks for reading!🤗