Recursion
Lesson Overview
# Understanding Recursion in JavaScript
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. 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 may involve 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
numbecomes less than or equal to 0, the function prints “Blastoff!” and stops calling itself. - Recursive case: The function prints the current
numand calls itself withnum - 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
nis 0 or 1, the function returns 1. - Recursive case: The function multiplies
nby the factorial ofn - 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:
Originally from Exercism javascript concepts