Lesson 11 of 30
If the word 'recursion' makes you tense up, that's a completely normal first reaction — here's the idea, stripped down to something small enough to actually follow.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
If the word "recursion" makes your stomach drop a little, you're not alone. Most people's first reaction to recursion is confusion, not understanding — and that's normal, not a sign you're bad at this. It genuinely feels strange the first time. It stops feeling strange with practice, not with talent.
Here's the plain version: recursion is a function that calls itself, working on a smaller piece of the problem each time, until the piece is so small there's nothing left to do — at which point it stops.
Think of a set of Russian nesting dolls. You open the big doll, and inside is a slightly smaller doll. You open that one, and inside is a smaller one still. You keep going until you reach the smallest doll — the one that doesn't open, because there's nothing inside it. That smallest doll is the signal to stop.
A recursive function works the same way. Every call opens up a slightly smaller version of the same problem, and hands that smaller version to itself. Eventually you reach the smallest version — simple enough to answer directly, with no further calls needed. That stopping point has a name: the base case. Without one, the dolls never stop opening, and the function would call itself forever (until it crashes).
function countdown(n) {
// The base case: the smallest doll. No more opening required.
if (n <= 0) {
console.log("Liftoff!");
return;
}
console.log(n);
// The function calls ITSELF, with a smaller number.
// This is the "open the next, smaller doll" step.
countdown(n - 1);
}
countdown(3);
// logs: 3, 2, 1, Liftoff!
countdown would call itself forever, going to -1, -2, -3... until the program runs out of memory and crashes with a "stack overflow." That name isn't a coincidence — it's the call stack from the last lesson, filling up.n. People often picture one shared n being edited. It's not — countdown(3) and the countdown(2) it calls each have their own separate copy of n, sitting on the call stack, waiting.Recursion doesn't need any new JavaScript syntax — it's an ordinary function, the same kind from the JavaScript track, that happens to call itself. What's new isn't the mechanism. It's learning to trust the pattern, which the next lesson tackles head-on.