codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

20 / 65 topics
14JavaScript Functions15JavaScript Function Expressions16JavaScript Arrow Functions17JavaScript Variable Scope18JavaScript Hoisting19JavaScript Closures20JavaScript Recursion
Tutorials/JavaScript/JavaScript Recursion
🌐JavaScript

JavaScript Recursion

Updated 2026-05-12
30 min read

JavaScript Recursion

Recursion is a powerful concept in programming where a function calls itself repeatedly until it reaches a specific condition known as the base case. This technique is particularly useful for solving problems that can be broken down into smaller, similar subproblems. In this tutorial, we'll explore how to implement recursion in JavaScript, understand its components like base cases and recursive steps, and see practical examples of its use.

Introduction

Recursion allows functions to call themselves repeatedly, which can simplify complex problems by breaking them down into simpler, more manageable parts. This approach is often used in algorithms that involve repetitive tasks or hierarchical data structures, such as traversing trees or calculating factorials. Understanding recursion is essential for advanced programming techniques and problem-solving.

Core Content

What is Recursion?

Recursion occurs when a function calls itself directly or indirectly. Each recursive call makes progress toward the base case, which is a condition that stops further recursion. Without a proper base case, recursion can lead to infinite loops, causing stack overflow errors.

Components of Recursion

  1. Base Case: The condition under which the recursion stops. It prevents the function from calling itself indefinitely.
  2. Recursive Step: The part of the function where it calls itself with modified parameters, moving closer to the base case.

Example: Factorial Calculation

Let's start with a simple example of calculating the factorial of a number using recursion.

factorial.js
1function factorial(n) {
2if (n === 0 || n === 1) { // Base case
3 return 1;
4}
5return n * factorial(n - 1); // Recursive step
6}
7
8console.log(factorial(5)); // Output: 120
Output
120

In this example, the factorial function calls itself with a decremented value of n until it reaches the base case (n === 0 || n === 1). The recursive step multiplies n by the result of the next recursive call.

Example: Fibonacci Sequence

Another classic problem that can be solved using recursion is generating the Fibonacci sequence.

fibonacci.js
1function fibonacci(n) {
2if (n <= 1) { // Base case
3 return n;
4}
5return fibonacci(n - 1) + fibonacci(n - 2); // Recursive step
6}
7
8console.log(fibonacci(7)); // Output: 13
Output
13

Here, the fibonacci function calls itself to calculate the sum of the two preceding numbers in the sequence until it reaches the base case (n <= 1).

Common Mistakes

  1. Missing Base Case: Failing to include a base case can lead to infinite recursion and stack overflow.
  2. Incorrect Recursive Step: The recursive step must ensure that the function makes progress toward the base case; otherwise, it may not terminate.

Warning

Always ensure that your recursive functions have a well-defined base case to prevent infinite loops.

Practical Example

Let's create a practical example of using recursion to flatten an array. This involves recursively traversing nested arrays and extracting their elements into a single-level array.

flattenArray.js
1function flattenArray(arr) {
2let result = [];
3
4arr.forEach(item => {
5 if (Array.isArray(item)) {
6 result = result.concat(flattenArray(item)); // Recursive call
7 } else {
8 result.push(item);
9 }
10});
11
12return result;
13}
14
15const nestedArray = [1, [2, [3, 4], 5], 6];
16console.log(flattenArray(nestedArray)); // Output: [1, 2, 3, 4, 5, 6]
Output
[1, 2, 3, 4, 5, 6]

In this example, the flattenArray function checks if each element is an array. If it is, the function calls itself recursively to flatten that subarray and concatenates the result with the current result array.

Summary

  • Recursion is a technique where a function calls itself repeatedly.
  • It consists of a base case to stop recursion and a recursive step to make progress toward the base case.
  • Common mistakes include missing base cases or incorrect recursive steps.
  • Recursion can simplify complex problems by breaking them down into smaller, similar subproblems.

What's Next?

In the next tutorial, we'll explore JavaScript strings, including string manipulation methods and common operations. Understanding strings is crucial for many programming tasks, such as data processing and user input handling.


PreviousJavaScript ClosuresNext JavaScript Strings

Recommended Gear

JavaScript ClosuresJavaScript Strings