Recursion
Recursion is a function calling itself (or a sibling) to solve a smaller piece of the same problem. Every recursive function needs a base case that does not call itself.
Detailed Explanation
Some problems shrink into the same problem: walk a tree, flatten nested arrays, compute n!, search a graph. Recursion matches that shape. You handle the empty or one-element case, then combine the result of a smaller call.
Two parts must be present. The base case returns without recursing. The recursive case calls the function on a strictly smaller input (a shorter array, a child node, n - 1). If the input does not shrink, the call stack grows until RangeError: Maximum call stack size exceeded. That error is the engine's stack limit, typically in the thousands of frames, not a spec number.
JavaScript does not guarantee tail-call optimisation. A "tail recursive" factorial still consumes O(n) stack in V8 and JavaScriptCore. If depth can be large, write a loop. Recursion is for depth you can bound (tree height, grammar nesting) or for code clarity on small n.
Mutual recursion is two functions calling each other. The same rules apply: a measure must decrease. Indirect cycles without a base case blow the stack the same way.
The call stack holds each frame's locals until the call returns. That is why a recursive DFS uses O(height) extra space, and why a deep JSON object can overflow JSON.parse internals or your own walker. An explicit stack array turns the same algorithm into a loop with heap memory, which is larger than the call stack.
Closures plus recursion are fine: an inner helper that closes over visited is the usual DFS. The helper is still subject to the stack limit.
What it costs you: stack overflows and harder debugging. A loop's state is in variables you can log. A recursion's state is spread across frames. Prefer recursion when the data is already a tree or the math is defined recursively. Prefer a loop when the data is a flat list of length n.
Reach for recursion on trees, nested structures, backtracking, and divide-and-conquer. Reach for an explicit stack when you would recurse on a chain of length n.
Code Examples
function factorial(n) {
if (n <= 1) return 1; // base case
return n * factorial(n - 1);
}
console.log(factorial(5)); // => 120function flatten(value) {
if (!Array.isArray(value)) return [value];
return value.flatMap(flatten);
}
console.log(flatten([1, [2, [3]], 4])); // => [ 1, 2, 3, 4 ]function walk(node, visit) {
if (node == null) return;
visit(node.value);
walk(node.left, visit);
walk(node.right, visit);
}
const tree = {
value: 2,
left: { value: 1, left: null, right: null },
right: { value: 3, left: null, right: null },
};
const out = [];
walk(tree, (v) => out.push(v));
console.log(out); // => [ 2, 1, 3 ]Common Gotchas
No shrinking input means a stack overflow
`function f(n) { return f(n); }` is the minimal crash. Check the base case first in a review.
Tail calls still consume stack in JS engines
convert deep linear recursion to a loop.
Shared mutable state across calls needs a new copy or an undo
backtracking that pushes to an array must `pop` after the recursive call.
`array.reduce` is not recursion
it is a loop. `flatMap` of a recursive function *is* recursion.