🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Recursive Functions

AI & DATA SCIENCE // recursive-functions

A recursive function calls itself to solve a problem by breaking it into smaller sub-problems. Requires a base case to stop.

Syntax

function factorial(n) {
  if (n <= 1) return 1;    // base case
  return n * factorial(n - 1); // recursive case
}

Deep Dive Course

**Recursion** is a pattern where a function calls itself. Every recursive function needs a **base case** (the stopping condition) and a **recursive case** (the call that moves toward the base case). Without a base case, recursion causes a **stack overflow**.

1Understanding Recursive Functions

Recursion is a pattern where a function calls itself. Every recursive function needs a base case (the stopping condition) and a recursive case (the call that moves toward the base case). Without a base case, recursion causes a stack overflow.

💡

JavaScript's call stack has a limit (~10,000 frames). For very deep recursion, use tail call optimization or an iterative approach.

editor.html
// Classic factorial
function factorial(n) {
  if (n <= 1) return 1;          // base case
  return n * factorial(n - 1);  // recursive case
}
console.log(factorial(5)); // 120
console.log(factorial(10)); // 3628800
localhost:3000

2Practical Example

Here is a real-world application of Recursive Functions showing how it is used in production JavaScript code.

editor.html
// Fibonacci with memoization
const memo = {};
function fib(n) {
  if (n <= 1) return n;
  if (memo[n]) return memo[n];
  memo[n] = fib(n - 1) + fib(n - 2);
  return memo[n];
}
console.log(fib(10)); // 55
console.log(fib(40)); // 102334155 (fast with memo)
localhost:3000

3Best Practices

Follow these guidelines when working with Recursive Functions:

1. Always define a clear base case first

2. Ensure each recursive call gets closer to the base case

3. Consider memoization for recursive functions with repeated sub-problems

⚠️

Tip: JavaScript's call stack has a limit (~10,000 frames). For very deep recursion, use tail call optimization or an iterative approach.

editor.html
// Classic factorial
function factorial(n) {
  if (n <= 1) return 1;          // base case
  return n * factorial(n - 1);  // recursive case
}
console.log(factorial(5)); // 120
console.log(factorial(10)); // 3628800
localhost:3000

Examples

Example 01Basic Usage
// Classic factorial
function factorial(n) {
  if (n <= 1) return 1;          // base case
  return n * factorial(n - 1);  // recursive case
}
console.log(factorial(5)); // 120
console.log(factorial(10)); // 3628800
Example 02Advanced Example
// Fibonacci with memoization
const memo = {};
function fib(n) {
  if (n <= 1) return n;
  if (memo[n]) return memo[n];
  memo[n] = fib(n - 1) + fib(n - 2);
  return memo[n];
}
console.log(fib(10)); // 55
console.log(fib(40)); // 102334155 (fast with memo)

Best Practices

  • Always define a clear base case first
  • Ensure each recursive call gets closer to the base case
  • Consider memoization for recursive functions with repeated sub-problems

Interview Question

What causes a stack overflow in recursion?

Hint: Call stack depth limit.

Each function call consumes a stack frame. Without a reachable base case, the recursive calls never stop, consuming stack frames until the engine's maximum call stack depth is exceeded, throwing a RangeError: Maximum call stack size exceeded.

Exercises

MediumPractice using Recursive Functions in a real scenario.
View Solution
// Classic factorial
function factorial(n) {
  if (n <= 1) return 1;          // base case
  return n * factorial(n - 1);  // recursive case
}
console.log(factorial(5)); // 120
console.log(factorial(10)); // 3628800

Frequently Asked Questions

What causes a stack overflow in recursion?

Each function call consumes a stack frame. Without a reachable base case, the recursive calls never stop, consuming stack frames until the engine's maximum call stack depth is exceeded, throwing a RangeError: Maximum call stack size exceeded.

Related Functions

Function-DeclarationReturnClosures