Frontend Interview Questions

JavaScript Event Loop Mechanism Explained

"Can you explain how the JavaScript event loop works?"

Why Interviewers Ask This

The event loop is the foundation of JavaScript's asynchronous programming model. Understanding it is crucial for writing correct async code, debugging timing issues, and answering the many "what will this output?" questions that appear in frontend interviews at every major tech company.

How to Answer

  1. 1

    Explain that JavaScript is single-threaded — it has one call stack and cannot execute multiple pieces of code simultaneously.

  2. 2

    Describe the event loop cycle: execute all synchronous code in the call stack → process all microtasks (Promise.then, MutationObserver, queueMicrotask) → process one macrotask (setTimeout, setInterval, I/O, UI rendering) → repeat.

  3. 3

    Emphasize microtask priority: all microtasks are processed before the next macrotask, and new microtasks added during microtask processing are also processed in the same cycle.

  4. 4

    Cover the callback queue (macrotask queue) vs. microtask queue distinction.

  5. 5

    Explain async/await as syntactic sugar over Promises — await pauses execution within the async function, but the code after await goes into the microtask queue.

Sample Answer

JavaScript runs on a single thread with one call stack, meaning it can only execute one piece of code at a time. The event loop is the mechanism that enables asynchronous behavior despite this single-threaded nature. Here is how it works: first, the engine executes all synchronous code on the call stack. When async operations like setTimeout or fetch are encountered, they are handed off to Web APIs (browser) or libuv (Node.js), which handle them in separate threads. When an async operation completes, its callback is placed in a queue. There are two types of queues with different priorities. The microtask queue (for Promise.then/catch/finally, MutationObserver, queueMicrotask) has higher priority — after the call stack is empty, ALL pending microtasks are processed before anything else. If a microtask adds another microtask, it is also processed in the same cycle. The macrotask queue (for setTimeout, setInterval, setImmediate, I/O callbacks, UI events) has lower priority — only ONE macrotask is processed per event loop iteration, after which the microtask queue is drained again. So the cycle is: call stack → all microtasks → one macrotask → all microtasks → one macrotask → and so on. With async/await: code before the first await runs synchronously; the await expression and everything after it behaves like .then() — it goes into the microtask queue. For example: console.log(1); setTimeout(() => console.log(2), 0); Promise.resolve().then(() => console.log(3)); console.log(4); outputs 1, 4, 3, 2 — synchronous code first (1, 4), then microtask (3), then macrotask (2).

Pro Tips

  • Be ready to trace through code output questions — they are the most common event loop interview format.

  • Remember: Promise constructor callback is synchronous, only .then/.catch/.finally are microtasks.

  • Know that async function code before the first await is synchronous.

  • Use HireMe AI during interviews to verify your understanding of complex async execution order.

Frequently Asked Questions

What is the output of the classic example with setTimeout(0) and Promise?

Synchronous code runs first, then Promise microtasks, then setTimeout macrotasks — even with a 0ms delay.

Are all Promises microtasks?

The Promise constructor runs synchronously. Only .then(), .catch(), .finally() callbacks are microtasks. Also, the code after await is a microtask.

Is requestAnimationFrame a microtask or macrotask?

Neither exactly. rAF callbacks run before the next paint, after microtasks but at a specific point in the rendering pipeline. It is not in the regular macrotask queue.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free