Recursion in JavaScript
1 0
Read Time:1 Minute, 57 Second

Recursion in JavaScript

Hello, Flocks👋

Javascript is a well-known Programming Language. Millions of websites around the world use JS.

In this post, you will learn about recursion in Javascript with the help of examples.

What Is Recursion?

Recursion is a process of calling itself. A Function that calls itself is known as a recursive function.

The syntax for the recursive function is:

function Demo_recurse_function(){
 // function code
  Demo_recurse_function();
 // function code
}

Demo_recurse_function();

A recursive function must have a condition to stop calling itself or else the function is called forever. and when the condition is met, the function stops calling itself also known as a base condition.

How to Stop Recursion?

To prevent infinite Recursion, you can use the if…else statement (or any similar approach) where one branch makes the recursive call and the other doesn’t.

Example to stop recursive function:

function Demo_recurse_function(){ 
 if(condition){
  Demo_recurse_function();
 }
 else{
  // stop calling Demo_recurse_function();
 }
} 

Demo_recurse_function();

Example: Find Factorial

A simple example of a recursion function would be to Find the Factorial of a number.

//Program to find factorial of a number
function factorial(x){
    //if number is 0
    if(x === 0){
        return 1;
    }
    //if a number is positive
    else{
        return x * factorial(x - 1);
    }
}

const number = 3;

// calling factorial function if num is positive
if(num > 0){
    let result = factorial(num);
    console.log(`The factorial of ${num} is ${result}`);
}

//The factorial of 3 is 6

This process continues until the number becomes 1. and when the number becomes 0, 1 is returned.

This recursive call can be explained in the following steps:

factorial(3) returns 3 * factorial(2)
factorial(2) returns 3 * 2 * factorial(1)
factorial(1) returns 3 * 2 * 1 * factorial(0)
factorial(0) returns 3 * 2 * 1 * 1

♥️ If you find it useful, please express your appreciation by giving it a like! (P.S. -It won’t cost anything don’t worry just go for it 😉)

✍️ Feel free to comment your thoughts and queries (Trust me it motivates me a lot 💎)

📥 Don’t forget to Bookmark it for later use (Or else you’ll miss it 🥲)

📲 Also share it with your friends and colleagues out there (Let’s help each other grow 👊🏻)

Do Follow Us on:

Facebook.com

Twitter.com

ALSO READ: CSS Pseudo Elements – A Quick Guide

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

By Akash Kothari

I am passionate about my work. Because I love what I do, I have a steady source of motivation that drives me to do my best. In my last job, this passion led me to challenge myself daily and learn new skills that helped me to do better work

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “Recursion in JavaScript

Leave a Reply

Discover more from TechDevil

Subscribe now to keep reading and get access to the full archive.

Continue reading