Event Loop Questions That Trip Up Senior Developers
Master the JavaScript event loop concepts that separate junior from senior engineers in technical interviews.
Event Loop Questions That Trip Up Senior Developers
The JavaScript event loop is one of the most fundamental concepts that can make or break a technical interview. Even senior developers often struggle to explain it clearly under pressure. Here's what you need to know to nail these questions.
Why Interviewers Love Event Loop Questions
Event loop questions test multiple skills at once:
- Deep JavaScript knowledge beyond syntax
- Ability to trace code execution step by step
- Understanding of asynchronous programming
- Communication skills when explaining complex concepts
The Classic Trap Question
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
What gets printed?
Most candidates guess: 1, 2, 3, 4
or 1, 4, 2, 3
Correct answer: 1, 4, 3, 2
Why This Trips People Up
The key insight: Not all async operations are equal
- Microtasks (Promises) have higher priority than macrotasks (setTimeout)
- Microtasks run after the current script but before any macrotasks
- Even
setTimeout(..., 0)
gets queued as a macrotask
Step-by-Step Execution
// 1. Synchronous code runs first
console.log('1'); // Prints: 1
// 2. setTimeout queued as macrotask
setTimeout(() => console.log('2'), 0);
// 3. Promise queued as microtask
Promise.resolve().then(() => console.log('3'));
// 4. More synchronous code
console.log('4'); // Prints: 4
// 5. Call stack empty, check microtask queue
// Promise callback runs -> Prints: 3
// 6. Microtask queue empty, check macrotask queue
// setTimeout callback runs -> Prints: 2
Advanced Variation They'll Ask Next
console.log('start');
setTimeout(() => console.log('timeout'), 0);
Promise.resolve()
.then(() => {
console.log('promise1');
return Promise.resolve();
})
.then(() => console.log('promise2'));
console.log('end');
Answer: start, end, promise1, promise2, timeout
The Framework for Any Event Loop Question
- Identify synchronous code - runs immediately
- Separate microtasks from macrotasks
- Microtasks: Promises, queueMicrotask()
- Macrotasks: setTimeout, setInterval, I/O
- Trace execution:
- All sync code first
- All microtasks
- One macrotask, then back to microtasks
How to Explain It in Interviews
"JavaScript has a single-threaded event loop with two main queues. The microtask queue has higher priority than the macrotask queue. After each macrotask, the engine processes all available microtasks before moving to the next macrotask."
Practice Exercise
Try this one:
async function test() {
console.log('A');
await Promise.resolve();
console.log('B');
}
console.log('1');
test();
console.log('2');
Click for answer
Answer: 1, A, 2, B
The await
makes everything after it run as a microtask.
Key Takeaways for Interviews
- Practice tracing execution on paper
- Always mention microtask vs macrotask priority
- Draw the event loop diagram if asked
- Use precise terminology (call stack, task queues)
- Test your understanding with edge cases
What's Next?
Master these concepts with Engagy's interactive drills. Our AI provides instant feedback on your explanations and helps you practice the communication skills that impress interviewers.