Event Loop Interview Questions in JavaScript
The event loop is a top interview topic. Here are the most asked questions with answers and code.
Event Loop Interview Questions in JavaScript
The event loop is one of the most asked interview topics. Here are the common questions with answers and code.
Q1: What is the output?
console.log("1"); setTimeout(() => console.log("2"), 0); Promise.resolve().then(() => console.log("3")); console.log("4"); `` **Answer**: `1, 4, 3, 2`. Sync first, then microtasks, then macrotasks. ### Q2: What is the output? ```js setTimeout(() => console.log("a"), 0); Promise.resolve().then(() => console.log("b")); console.log("c"); `` **Answer**: `c, b, a`. Sync, microtask, macrotask. ### Q3: What is the output? ```js Promise.resolve().then(() => console.log("1")).then(() => console.log("2")); Promise.resolve().then(() => console.log("3")).then(() => console.log("4")); `` **Answer**: `1, 3, 2, 4`. Microtasks are FIFO. The first .then of each promise runs before the second .then. ### Q4: What is the output? ```js for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } `` **Answer**: `3, 3, 3`. `var` is function-scoped; all callbacks share one `i`. ### Q5: What is the output? ```js async function foo() { console.log("1"); await Promise.resolve(); console.log("2"); } foo(); console.log("3"); `` **Answer**: `1, 3, 2`. `await` pauses `foo` and puts the rest in a microtask. `3` logs first (sync), then `2` (microtask). ### Q6: What is the output? ```js console.log("start"); setTimeout(() => console.log("timeout"), 0); Promise.resolve().then(() => { console.log("promise1"); Promise.resolve().then(() => console.log("promise2")); }); console.log("end"); `` **Answer**: `start, end, promise1, promise2, timeout`. Nested microtasks run before the macrotask. ### Q7: Can microtasks block rendering? **Answer**: Yes. The event loop drains all microtasks before rendering. If microtasks take too long, the paint is delayed. This is why you should keep microtasks short. ### Q8: Can microtasks starve macrotasks? **Answer**: Yes. If a microtask keeps adding more microtasks, the event loop never reaches the macrotask queue. setTimeout callbacks never run. ### Q9: Is the event loop part of the JS engine? **Answer**: No. The event loop is part of the host environment (browser or Node.js). The JS engine (V8) just has the call stack and heap. The event loop, Web APIs, and queues are provided by the host. ### Q10: What is `process.nextTick` in Node.js? **Answer**: A Node.js-specific microtask that runs before promise callbacks. It has higher priority than Promise.then. Use it carefully; it can starve promises and the event loop. ### The Takeaway Event loop interview questions test: sync vs microtask vs macrotask ordering, nested microtasks, the loop closure bug, async/await timing, microtask starvation, and rendering. Remember: sync → all microtasks → one macrotask → microtasks → repeat.
1, 4, 3, 2. Sync code runs first (1, 4). Then all microtasks (3). Then one macrotask (2). This is the fundamental event loop order.
Code before await runs synchronously. await pauses the function and schedules the rest as a microtask. Code after the foo() call runs next (sync). Then the rest of foo runs (microtask). So: 1, 3, 2 for the example above.
Yes. The event loop drains all microtasks before rendering. If microtasks take too long, the paint is delayed. Keep microtasks short to maintain smooth rendering.
No. The event loop is part of the host environment (browser or Node.js). The JS engine (V8) has the call stack and heap. The event loop, Web APIs, and queues are provided by the host.
A Node.js-specific microtask that runs before promise callbacks. It has higher priority than Promise.then. Use it carefully; excessive nextTick calls can starve promises and the event loop.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

