Skip to main content

Command Palette

Search for a command to run...

🚀 Recursion in JavaScript: A Complete Beginner-Friendly Guide

🚀 Recursion in JavaScript

Published
•3 min read•View as Markdown
🚀 Recursion in JavaScript: A Complete Beginner-Friendly Guide

Recursion is one of the most powerful concepts in JavaScript—yet one of the most misunderstood. In simple words, recursion is a technique where a function calls itself until a base condition is reached.

It helps you solve problems that can be broken into smaller subproblems, such as factorials, Fibonacci numbers, searching, tree/graph traversal, and more.

In this blog, we’ll explore recursion with examples, and then provide practice questions with solutions to strengthen your understanding.


🔁 What is Recursion?

A recursive function is a function that calls itself.
Every recursive function must have:

✅ Base Case → stops recursion

✅ Recursive Case → function calls itself


🔢 Factorial of a Number (Using Recursion)

function factorial(n){
    if(n === 0)
        return 1;
    return n * factorial(n - 1);
}

console.log(factorial(8));

📌 How it works:

  • factorial(8) → 8 × factorial(7)

  • factorial(7) → 7 × factorial(6)

  • …

  • factorial(0) → 1 (base case)


➕ Sum of Elements in an Array (Recursive Approach)

function sumOfArrays(arr, n){
    if(n === 0){
        return 0;
    }

    return arr[n - 1] + sumOfArrays(arr, n - 1);
}

console.log(sumOfArrays([1, 2, 3, 4, 5], 5));

📌 Breaking the array step by step:
5 + sum([1,2,3,4])
4 + sum([1,2,3])
… until n === 0


🌀 Fibonacci Number (Recursive)

function fibo(n){
    if(n < 2){
        return n;
    }
    return fibo(n - 1) + fibo(n - 2);
}

console.log(fibo(5));

This is a classic recursion example.


🧠 Practice Questions (with Recursive Solutions)

Below are the exact problems you asked for—with clear recursive code.


1️⃣ Check if a String is Palindrome

function isPalindrome(str){
    if(str.length <= 1) return true;
    if(str[0] !== str[str.length - 1]) return false;
    return isPalindrome(str.slice(1, -1));
}

console.log(isPalindrome("madam"));

2️⃣ Implement pow(x, n) → xⁿ

function pow(x, n){
    if(n === 0) return 1;
    return x * pow(x, n - 1);
}

console.log(pow(2, 5));

3️⃣ Sum of Digits of a Number

Example: 453 → 4 + 5 + 3 = 12

function sumOfDigits(n){
    if(n === 0) return 0;
    return (n % 10) + sumOfDigits(Math.floor(n / 10));
}

console.log(sumOfDigits(453));

4️⃣ Count Digits in a Number

Example: 453 → 3

function countDigits(n){
    if(n === 0) return 0;
    return 1 + countDigits(Math.floor(n / 10));
}

console.log(countDigits(453));

5️⃣ Find LCM of Two Numbers (Using Recursion)

LCM formula:
LCM(a, b) = (a × b) / GCD(a, b)

So we first create GCD:

function gcd(a, b){
    if(b === 0) return a;
    return gcd(b, a % b);
}

function lcm(a, b){
    return (a * b) / gcd(a, b);
}

console.log(lcm(12, 18));

6️⃣ Find GCD of Two Numbers (Euclid’s Algorithm)

function gcd(a, b){
    if(b === 0) return a;
    return gcd(b, a % b);
}

console.log(gcd(48, 18));

7️⃣ Reverse a String (Using Recursion)

function reverseString(str){
    if(str === "") return "";
    return reverseString(str.slice(1)) + str[0];
}

console.log(reverseString("hello"));

🎯 Final Thoughts

Recursion is not just a programming technique—it’s a problem-solving mindset.

✔ Break the problem
✔ Use a base case
✔ Call the function again on a smaller input

Mastering recursion supercharges your logic-building ability and prepares you for coding interviews, especially at MAANG companies.