Facebook Pixel

What is a trampoline in JavaScript recursion?

A pattern that wraps recursive returns in thunks (functions) and runs them in a loop, so the stack does not grow. It lets you handle deep recursion without tail call optimization.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Recursive Functions and the Call Stack in JavaScript

Each recursive call pushes a new frame with its own local variables. The stack grows with depth. When the base case returns, frames pop in reverse order and return values combine on the way back up.

Without a base case, the function calls itself forever, frames pile up, and the engine throws RangeError: Maximum call stack size exceeded. The base case is what stops the recursion.

The ES6 spec includes it, but most engines (including V8 in Chrome and Node.js) do not implement it. In practice, deep recursion still overflows the stack and should be converted to iteration or use a trampoline.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0