Skip to content

Latest commit

 

History

History
93 lines (62 loc) · 2.64 KB

introduction.md

File metadata and controls

93 lines (62 loc) · 2.64 KB

Understanding Recursion in JavaScript

Recursion is a powerful concept in programming that involves a function calling itself. It can be a bit tricky to grasp at first, but once you understand the fundamentals, it becomes a valuable tool in solving complex problems. In this tutorial, we'll explore recursion in JavaScript with easy-to-understand examples.

What is Recursion?

Recursion occurs when a function calls itself, either directly or indirectly. It's similar to a loop, but it involves breaking a problem down into smaller, more manageable sub-problems.

Example 1: Countdown

Let's start with a simple example: a countdown function.

function countdown(num) {
  // Base case
  if (num <= 0) {
    console.log("Blastoff!");
    return;
  }

  // Recursive case
  console.log(num);
  countdown(num - 1);
}

// Call the function
countdown(5);

In this example:

  • Base case: When num becomes less than or equal to 0, the function prints "Blastoff!" and stops calling itself.
  • Recursive case: The function prints the current num and calls itself with num - 1.

Example 2: Factorial

Now, let's look at a classic example of recursion: calculating the factorial of a number.

function factorial(n) {
  // Base case
  if (n === 0 || n === 1) {
    return 1;
  }

  // Recursive case
  return n * factorial(n - 1);
}

// Test the function
console.log(factorial(5)); // Output: 120

In this example:

  • Base case: When n is 0 or 1, the function returns 1.
  • Recursive case: The function multiplies n by the factorial of n - 1.

Key Concepts

Base Case

Every recursive function should have at least one base case, a condition where the function stops calling itself. Without a base case, the recursion would continue indefinitely, leading to a stack overflow.

Recursive Case

The recursive case defines how the function calls itself with a smaller or simpler version of the problem.

Pros and Cons of Recursion

Pros:

  • Elegant solution for certain problems.
  • Mimics the mathematical induction concept.

Cons:

  • Can be less efficient than iterative solutions.
  • May lead to stack overflow for deep recursion.

Conclusion

Recursion is a valuable technique that simplifies complex problems by breaking them into smaller, more manageable sub-problems. Understanding base cases and recursive cases is crucial for implementing effective recursive solutions in JavaScript.

Learn More: