Technical Mid Level

Explain the JavaScript event loop. How do microtasks and macrotasks differ, and why does this matter for application behaviour?

Quick Tip

Use a concrete example: "After a click handler runs, all queued Promise callbacks execute before the browser repaints or fires the next setTimeout. That is why an infinite chain of microtasks can freeze the UI."

What good answers include

JavaScript is single-threaded with an event loop that processes a call stack, a microtask queue (Promises, queueMicrotask, MutationObserver), and a macrotask queue (setTimeout, setInterval, I/O). After each macrotask, all microtasks are drained before the next macrotask runs. This means Promise.then callbacks execute before setTimeout callbacks even if setTimeout was scheduled first. Strong candidates explain how this affects UI rendering (microtasks can block repainting) and Node.js I/O scheduling.

What interviewers are looking for

Core JavaScript knowledge. Candidates who cannot explain the event loop will write code with subtle timing bugs. Ask them to predict the output order of a mix of console.log, setTimeout, and Promise.resolve calls.

← All JavaScript questions