/ /

Module 1 of 160/7 done112 lessons left

The call stack never lies

1 +45
045s
🦊lv 1
0/112 · 0%
10 min focus gate 45s streak-safe session

The call stack never lies

Module 1: The Execution Model

The call stack never lies

Every function call is a frame; when the stack is busy nothing else on the thread can interrupt it.

JavaScript runs one statement at a time on one thread. Everything that feels concurrent — animation, fetch responses, click handlers — is choreographed by a loop that decides what runs next. Learn that loop and you can predict behaviour instead of guessing.

analogy / One chef, two ticket rails

The call stack is a chef taking one order at a time. The microtask queue is a VIP rail beside the stove, cleared completely the moment hands are free. The macrotask queue is the normal rail — one ticket per round. Rendering is the dining room: served only when the kitchen is not backed up.

1. A frame is pushed when a function is called and popped when it returns — last in, first out, one stack per thread.

2. While a frame is executing there is no preemption: timers, clicks and network data all wait until the stack is empty again.

3. Stack overflow happens when a frame calls itself without a base case — the engine throws once it runs out of room.

javascript
01
02
03
04
05
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
// A D C B
Trace every line before you run it.

predict 01 · commit before reveal

+25 xp

A 30ms synchronous function runs inside a click handler. When can a queued timeout callback execute?

QueueLives hereRuns when
Call stacksynchronous callsimmediately, LIFO
Microtaskspromises, queueMicrotaskdrained fully after each task
Macrotaskstimers, I/O, UI eventsone per loop turn
Renderstyle, layout, paintwhen the loop lets it

The call stack never lies — interactive simulator

4-frame window

Every function call is a frame; when the stack is busy nothing else on the thread can interrupt it.

0ms67ms = 4 frames
f1
f2
f3
f4
■ blocking task■ paint (dimmed = dropped)
short task22mslong task

22ms task → 1 frame dropped

Skipped paint → scroll jank. Split the work or defer it.

mind map · one idea, many anchors

The call stack never lies
  • 1A frame is pushed when a function is called and popped when it returns — last in, first out, one stack per thread.
  • 2While a frame is executing there is no preemption: timers, clicks and network data all wait until the stack is empty again.

Call stack, queues, rendering opportunities.

memory hook · short trick

Stack, VIP, one, paint

Sync drains first, then the microtask VIP rail empties completely, then exactly one macrotask, then the browser may paint. That sequence never varies.

formula / latexscaled dot-product
Fdropped​=⌈Ttask​/16.67ms⌉−1
Frames skipped by a single synchronous task.
🎮

skill game · 30 seconds · real points

+30 xp

Which habit most improves retention a week after learning something?

0%

Completion gate

45s of active reading left

0/45s checkpoint 0/1 lab 0%

Verified focus.The timer pauses when this tab loses visibility, so the gate can't be farmed.

Need a break? Bank the streak with an arena duel — timed challenges keep the multiplier alive.

live code playground · practice while you read

Pre-loaded with the snippet from "The call stack never lies". Tweak values, log intermediate steps, or test your own idea.

scratch.js real runner
01
02
03
04
05
06
07
08
09

stdout / stderr

[runner] live practice editor · node 22 / python 3

[runner] edit the chapter snippet or write your own, then press Run code

isolated runner · assertions only
reading · none tutor cites this lesson