Skip to content

The stack

A call is a frame

Every call pushes a frame with its own locals; every return pops one. A throw pops several. · 8 minutes

Calling a function does not jump to it. It pushes a frame: a fresh set of bindings for that call's parameters and locals, and a note of where to come back to. The function's body runs inside that frame, and `return` pops it and hands one value back to the line that called.

Two calls to the same function are two frames. Their locals have the same names and nothing else in common — which is also why recursion works at all.

function sum(n) { if (n === 0) return 0; return n + sum(n - 1); } — at the moment sum(3) reaches return 0, how many sum frames are on the stack?