The Two Parts Every Recursive Function Needs

factorial.js function factorial(n) { if (n <= 1) return 1; // base case — stops the recursion return n * factorial(n - 1); // recursive case — calls itself with smaller input } factorial(5); // 5 * 4 * 3 * 2 * 1 = 120
  • Base case: the simplest possible input, answered directly without another call. Without one, the function recurses forever and crashes.
  • Recursive case: calls itself with an input that is strictly closer to the base case.

What Happens on the Call Stack

Calling factorial(5) pushes a new stack frame for every call, then unwinds as each call returns:

call stack growth and unwind factorial(5) -> factorial(4) -> factorial(3) -> factorial(2) -> factorial(1) returns 1 returns 2 * 1 = 2 returns 3 * 2 = 6 returns 4 * 6 = 24 returns 5 * 24 = 120

Each arrow down is a new stack frame being pushed; each "returns" is a frame popping off as the result bubbles back up.

⚠️ Forgetting the Base Case = Stack Overflow

If factorial never checked n <= 1, it would call itself indefinitely — eventually exceeding the call stack's size limit and crashing with a "Maximum call stack size exceeded" (or equivalent) error.

Recursion vs Iteration: Same Problem, Two Approaches

AspectRecursionIteration
MechanismFunction calls itselfLoop repeats a block of code
MemoryUses call stack — O(n) extra space for n callsUsually O(1) extra space
ReadabilityOften clearer for naturally recursive problemsOften clearer for simple linear processing
RiskStack overflow on deep recursionNo stack risk
Best forTrees, graphs, divide-and-conquerFlat lists, simple counters, accumulation

The Same Logic, Two Ways

factorial — iterative version function factorialIterative(n) { let result = 1; for (let i = 2; i <= n; i++) { result *= i; } return result; }

Both versions compute the same answer. The iterative version uses a single stack frame and a loop counter; the recursive version expresses the mathematical definition of factorial almost word-for-word (n! = n * (n-1)!).

Why Fibonacci Shows Recursion's Weakness

naive fibonacci — exponential time function fib(n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); // recomputes the same values repeatedly }

Naive recursive Fibonacci recalculates the same sub-problems over and over — fib(5) calls fib(3) twice, fib(2) three times, and so on — leading to exponential O(2ⁿ) time. This is exactly the kind of waste that memoization fixes by caching results of sub-problems already computed, and it connects directly to Big O analysis when reasoning about algorithm cost.

Tail Call Optimization

A tail call is a recursive call that is the very last operation in a function — nothing happens after it returns. Some languages (Scheme, Erlang) guarantee tail call optimization: they reuse the current stack frame instead of pushing a new one, making tail-recursive functions run in constant stack space, just like a loop.

tail-recursive factorial function factorialTail(n, acc = 1) { if (n <= 1) return acc; return factorialTail(n - 1, n * acc); // last operation — a true tail call }

⚠️ JavaScript Mostly Doesn't Optimize Tail Calls

Despite being specified in ES2015, most JavaScript engines (including V8/Chrome/Node) never shipped tail call optimization. Writing tail-recursive JS code does not protect you from stack overflows the way it would in Scheme or Erlang.

💡 Rule of Thumb

If you can express the problem as "solve a smaller version of this same problem, then combine," reach for recursion. If you're just walking through a flat sequence once, a loop is simpler and safer.

How We Research and Update This Guide

We test the underlying formula or workflow, compare outputs with reliable references, and revise examples whenever the page content changes.

  • The workflow or formula is tested directly in the tool and compared against independent reference examples.
  • Examples are kept practical so readers can verify the result without hidden assumptions.
  • Pages are revised whenever the interface, calculation flow, or surrounding guidance materially changes.

Frequently Asked Questions — Recursion