Blog1 min readPremium

The JavaScript Event Loop Explained With One Diagram

The JavaScript Event Loop Explained With One Diagram

“Explain the event loop” is one of the most asked JavaScript interview questions I’ve seen. It came up in some form in almost every loop I went through, either directly or hidden inside an “what does this code print?” puzzle.

Most explanations I read when I was preparing were either too shallow (“JavaScript is single-threaded and uses callbacks”) or went straight into the HTML spec. Here’s the version I wish I’d had: one diagram, the rules that actually matter, and the puzzles interviewers love.

The diagram

One turn of the browser event loop: run a task, drain microtasks, render, with the call stack, task queue, and microtask queue

JavaScript runs on one main thread with one call stack. Things like timers, network requests, and DOM events are handled by the browser outside of JavaScript. When they’re ready, they don’t interrupt your code. They put a callback in a queue, and the event loop picks it up when the stack is empty.

The whole thing comes down to three rules.

Rule 1: Run one task to completion

A task (often called... premium content.

Premium

Keep reading with Premium

Get the rest of this guide plus every premium question, solution, and system design deep dive.

  • All premium solutions
  • System design deep dives
  • Company-specific prep

4.9/5 from our users