Skip to main content
🔁 Recursion

When You Need Two Base Cases

Everything so far had one base case and made one recursive call. Some problems need more. Fibonacci needs two base cases and makes two recursive calls.

The sequence starts 0, 1, 1, 2, 3, 5, 8, ... — each number is the sum of the two before it. So you need to know the two smallest answers outright:

function fib(n) {
  if (n < 2) return n;             // base cases: fib(0)=0, fib(1)=1
  return fib(n - 1) + fib(n - 2);  // TWO recursive calls, summed
}

if (n < 2) return n is a tidy way to cover both base cases at once (fib(0) returns 0, fib(1) returns 1). And because each call spawns two more, the work branches into a tree — beautiful, but slow for large n.

Read it, then answer below. 👇

Why does fib need two base cases?

What does the single check if (n < 2) return n; handle?

How many recursive calls does each non-base fib(n) make?