JavaScript·Intermediate·Coding·1 min read
How does recursion fail with a stack overflow?
Short interview answer
Each unresolved synchronous recursive call consumes another stack frame. Without a base case, or with input deeper than the engine's stack capacity, the engine throws a range error. JavaScript engines are not generally required to optimize ordinary tail calls in deployed environments.
Example
function depth(n) { return depth(n + 1); } // no base casedepth(1); // RangeError: Maximum call stack size exceededKey takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.