Explain JavaScript Recursive Function
A recursive function in JavaScript is a function that calls itself in order to solve a problem. Recursion is a common technique in programming where a problem is divided into smaller, more manageable sub-problems of the same type. A base case is used to terminate the recursion to prevent it from calling itself indefinitely.
Here are two examples of recursive functions in JavaScript:
- Factorial Calculation
- Fibonacci Sequence
1. Factorial Calculation
The factorial of a number ( n ) (denoted as ( n! )) is the product of all positive integers less than or equal to ( n ). The factorial of 0 is 1.
Factorial Function (with Indentation):
function factorial(n) {
if (n === 0) {
return 1; // Base case: factorial of 0 is 1
}
return n * factorial(n - 1); // Recursive call
}
console.log(factorial(5)); // Output: 120
Explanation:
- If ( n ) is 0, the function returns 1.
- Otherwise, the function calls itself with ( n - 1 ) and multiplies the result by ( n ).
Output:
For factorial(5):
factorial(5)returns5 * factorial(4)factorial(4)returns4 * factorial(3)factorial(3)returns3 * factorial(2)factorial(2)returns2 * factorial(1)factorial(1)returns1 * factorial(0)factorial(0)returns1
Thus, the calculation is 5 * 4 * 3 * 2 * 1 = 120.
2. Fibonacci Sequence
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1.
Fibonacci Function (with Indentation):
function fibonacci(n) {
if (n === 0) {
return 0; // Base case: fibonacci of 0 is 0
}
if (n === 1) {
return 1; // Base case: fibonacci of 1 is 1
}
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive call
}
console.log(fibonacci(6)); // Output: 8
Explanation:
- If ( n ) is 0, the function returns 0.
- If ( n ) is 1, the function returns 1.
- Otherwise, the function calls itself with ( n - 1 ) and ( n - 2 ), and returns the sum of these two calls.
Output:
For fibonacci(6):
fibonacci(6)returnsfibonacci(5) + fibonacci(4)fibonacci(5)returnsfibonacci(4) + fibonacci(3)fibonacci(4)returnsfibonacci(3) + fibonacci(2)fibonacci(3)returnsfibonacci(2) + fibonacci(1)fibonacci(2)returnsfibonacci(1) + fibonacci(0)fibonacci(1)returns1fibonacci(0)returns0
Thus, the calculation is:
fibonacci(6) = 8fibonacci(5) = 5fibonacci(4) = 3fibonacci(3) = 2fibonacci(2) = 1fibonacci(1) = 1fibonacci(0) = 0
So, fibonacci(6) results in 8.
By understanding these examples, you can see how recursion breaks a problem down into smaller sub-problems and solves them individually, eventually combining their results to get the final answer.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
