Skip to the solution
HardFrontend6 min read

Explain the event loop, call stack, and task queues.

1.2k views

The problem

Question presented to candidate: "Walk me through what the call stack, the event loop, and the task queues actually are, and how they work together when JavaScript runs asynchronous code."

What a strong answer should cover:

  • The call stack is a real, finite, last-in-first-out (LIFO) structure of function frames -- the innermost call always finishes before the call that made it resumes.
  • JavaScript is single-threaded: only one frame executes at a time, so a long-running synchronous function blocks everything else, including timers and promise callbacks.
  • The event loop is the mechanism that, once the call stack is completely empty, checks the microtask queue (fully drains it), then runs exactly one macrotask, then repeats.
  • Two separate queues exist for queued async work: the microtask queue (Promise reactions, queueMicrotask) and the macrotask/task queue (setTimeout, setInterval, I/O, UI events).
  • A callback in either queue can only start running once the call stack is empty -- queued work never interrupts currently running synchronous code.

Clarifying questions expected:

  • "Do you want the browser event loop specifically, or should I also mention how Node.js differs (for example process.nextTick)?"

Code / implementation expected: Yes -- a short, runnable snippet demonstrating that a synchronous busy loop delays both a queued timer and a queued microtask.

  • event-loop

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 7 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptCall stack LIFO order, then a busy loop delaying a queued timer and microtask (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 119 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What does the RegExp `v` flag (Unicode Sets) add over the `u` flag?

Continue