Skip to the workspace
Pebellslearn by building
Functions  ›  Lesson

Scope and the Function (Call) Stack

Scope & Function Stack 12 min

javascript / Scope and the Function (Call) Stack

Explore each scene at your own pace.

01 · Scope and the Function (Call) Stack

Scope and the Function (Call) Stack

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

Walk through the modelScope and the Function (Call) StackAuthored snapshots of the example; use Run below to test the code.
total(minutes4)
Statewaiting for double
The expression needs double’s result before it can finish.
1 of 3

Follow one example

javascript

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.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.