Scope and the Function (Call) Stack
The call stack records active function invocations. A call pauses its caller until it returns or throws; scope determines which bindings each invocation can access.
Follow calls downward and returned results upward.
See the mechanism
4)↓waiting for doubleFollow one example
The total invocation waits for double(4). double returns 8, then total adds 1 and returns 9 to console.log. Each invocation has its own parameters and local bindings. The stack is an execution model, not a guarantee of a particular engine’s physical memory layout.
Predict, change and check
Predict: Will total add 1 before or after double has produced its result?
Test your explanation: Add logging at the start and just before return in each function. Predict the order, then check it without changing the calculations.
The tasks below ask you to explain that result and apply the idea to different inputs.
Apply and distinguish
One invocation, one active frame
A call stack records active invocations and where execution resumes after a call returns. Each invocation has its own local bindings even when the same function is called repeatedly or recursively. A stack frame is not the same thing as a long-lived heap object captured by a closure.
At a breakpoint, inspect which frame is selected before reading a variable's value in DevTools. Two frames can use the same parameter name with different values. A promise or timer callback registered now usually runs in a later execution turn after the current stack unwinds; it does not keep this synchronous stack paused in place.
Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.