🚀 We're building something amazing!Some features are still in development.
← Back to blog

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

  1. Microtasks (Promises) have higher priority than macrotasks (setTimeout)
  2. Microtasks run after the current script but before any macrotasks
  3. 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

  1. Identify synchronous code - runs immediately
  2. Separate microtasks from macrotasks
    • Microtasks: Promises, queueMicrotask()
    • Macrotasks: setTimeout, setInterval, I/O
  3. 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

  1. Practice tracing execution on paper
  2. Always mention microtask vs macrotask priority
  3. Draw the event loop diagram if asked
  4. Use precise terminology (call stack, task queues)
  5. 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.

Start practicing event loop questions →