Recursion is when a function calls itself to solve a smaller piece of the same problem, until it reaches a case simple enough to answer directly. Every javascript recursion needs two parts: a base case that stops the calls, and a recursive case that moves toward it. Each call is added to the call stack, which makes recursion elegant for trees and nested data but limited in depth.
This guide covers how calls stack up, practical examples, the stack overflow error, and the iterative alternatives.
A recursive function must always reach a point where it stops. Without a base case, it calls itself forever until the engine throws an error.
For factorial(5), the calls expand and then resolve:
Each pending multiplication waits on the call stack until the deeper call returns. For large results use BigInt, because ordinary numbers lose precision beyond Number.MAX_SAFE_INTEGER.
Every function call creates a stack frame that stores its arguments and local variables. When a function calls itself, a new frame is pushed. When the base case returns, frames are popped one by one.
The stack has a limited size. Calling too deep throws a RangeError:
The exact depth limit depends on the engine and the size of each frame, and is usually in the thousands to low tens of thousands of calls. Do not rely on a specific number.
Recursion fits naturally when the data itself is nested, such as folders, comments, menus or JSON trees, because the depth is not known in advance.
Each call handles one node and delegates its children to the same function. Another typical example flattens arbitrarily nested arrays, which Array.prototype.flat(Infinity) already does for you:
Naive recursion can repeat work. The classic Fibonacci function recomputes the same values many times, giving exponential time.
Memoization stores results already computed, so each value is calculated once and the running time becomes linear.
A tail call is a recursive call that is the last action of a function. The ES2015 specification asks engines to reuse the stack frame for such calls, but in practice only Safari (JavaScriptCore) implements it. V8 in Chrome, Node.js and Edge, and Firefox do not. Do not write code that depends on tail call optimisation.
When depth could be large, convert recursion to a loop with your own stack. This avoids the stack limit.
Here the explicit stack array replaces the call stack. Note that the visiting order is different (the last child is processed first); reverse the children when pushing if order matters.
Choose recursion when it makes the code clearer and depth is bounded, such as tree walks and parsers. Choose a loop when the input is large or untrusted, for instance user-supplied JSON that could be nested very deeply.
A recursive function that never reaches its base case, or input so deep that it needs more frames than the engine allows. Check the base case first, then consider a loop.
Usually slightly, because each call has overhead, and naive versions can repeat work. Memoization or a loop fixes most performance problems.
The specification includes it, but most engines, including V8 and SpiderMonkey, do not implement it. Treat it as unavailable.
Yes, if assigned to a variable that they refer to by name, such as const fact = (n) => (n <= 1 ? 1 : n * fact(n - 1));.
Recursion solves a problem by having a function call itself on smaller input until a base case returns. It suits nested and tree-shaped data, but each call uses stack space, so very deep recursion throws a RangeError. Use memoization to avoid repeated work, and switch to an iterative version with an explicit stack when the depth is unbounded.