JavaScript Runtime9h estimated

The Event Loop

Difficulty
Importance

The problem

JavaScript runs on a single thread, yet needs to handle slow operations (network requests, timers, file reads) without freezing everything else while waiting — the event loop is the mechanism that lets one thread stay responsive by never blocking on anything slow.

Why now

Processes and threads already covered multi-threaded concurrency as one answer to 'do many things at once'; JavaScript's runtime takes a different approach entirely, and understanding it requires the call stack and queue vocabulary from memory-and-the-stack and stacks-and-queues to describe precisely instead of hand-wavingly.

Mental model

JavaScript runs code on a single call stack, one frame at a time. When it hits something slow (a timer, a network call), it hands that off to the browser/Node runtime and moves on — when the slow thing finishes, its callback doesn't run immediately, it waits in a queue until the call stack is completely empty. The event loop's whole job is one repeated check: 'is the stack empty? if so, pull the next queued callback and run it.'

Requires

Helps

Unlocks

Used in

Projects

  • Write code mixing synchronous logs, setTimeout(fn, 0), and a resolved Promise's .then(), predict the execution order, then run it and explain any surprises
  • Write a deliberately blocking synchronous loop and observe how it freezes a page's UI until it completes

Examples

  • console.log('a'); setTimeout(() => console.log('b'), 0); console.log('c'); logs a, c, b — the timeout callback waits for the stack to empty even with a 0ms delay
  • A tight synchronous loop that never returns control to the event loop will freeze a browser tab entirely

Resources

Mastery checklist