Event Loop Questions with Answers

15 practice questions on Event Loop (JavaScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Easy

When does a callback passed to setTimeout(fn, 0) run?

  1. Only after every image and stylesheet on the page has finished loading
  2. Immediately, before the line after the setTimeout call is executed
  3. After the current code finishes and pending microtasks have run
  4. On a separate thread, in parallel with the code that scheduled it

Answer: C. After the current code finishes and pending microtasks have run

Explanation

setTimeout schedules a task (macrotask). It can run only once the call stack is empty and the microtask queue has been drained.

Step-by-step solution

  1. setTimeout(fn, 0) does NOT run fn immediately; it schedules fn as a macrotask ('task') to run later.
  2. Before that task can run, the JavaScript engine must first finish executing all currently running synchronous code.
  3. It must then also drain the entire microtask queue (any pending Promise .then/.catch callbacks, queueMicrotask callbacks) before finally picking up the next macrotask, including this timer's callback.

Why the other options are wrong

  • A. Waiting for every image/stylesheet to finish loading is unrelated to how setTimeout(fn, 0) is scheduled; it's governed by the task/microtask queue system, not resource loading.
  • B. Running immediately, before the very next line, contradicts how setTimeout works entirely - even a 0ms delay still defers execution to a later point in the event loop.
  • D. Running on a separate thread in parallel describes Web Workers, not setTimeout callbacks, which always run on the same single JavaScript main thread.

Tip: setTimeout(fn, 0) really means 'run fn as soon as possible AFTER the current call stack and all pending microtasks are done', not 'run fn right now'.

Question 2Easy

Which of these schedules a microtask?

  1. A click event handler
  2. setTimeout(fn, 0)
  3. queueMicrotask(fn)
  4. setInterval(fn, 10)

Answer: C. queueMicrotask(fn)

Explanation

queueMicrotask and promise reactions (then/catch/finally, await continuations) are microtasks. Timers and UI events are tasks.

Step-by-step solution

  1. JavaScript has two kinds of 'deferred work' queues: the microtask queue and the (macro)task queue, each with different priority.
  2. queueMicrotask(fn) explicitly schedules fn to run as a microtask, in the same high-priority queue that Promise .then/.catch/.finally callbacks and async/await continuations use.
  3. setTimeout, setInterval and UI event handlers (like clicks) all schedule their callbacks as macrotasks (regular tasks), a lower-priority queue than microtasks.

Why the other options are wrong

  • A. A click event handler is scheduled as a macrotask (a regular task), not a microtask.
  • B. setTimeout(fn, 0) schedules a macrotask, even with a 0ms delay.
  • D. setInterval(fn, 10) also schedules its repeating callback as a macrotask each time it fires, just like setTimeout.

Tip: If you need something to run as soon as possible, even before the next timer fires, queueMicrotask() guarantees microtask-queue priority.

Question 3Easy

What is the output?

console.log('A');
setTimeout(() => console.log('B'), 0);
console.log('C');
  1. A, B, C
  2. B, A, C
  3. C, A, B
  4. A, C, B

Answer: D. A, C, B

Explanation

Synchronous code runs first (A, C); the zero-delay timer callback runs later from the task queue.

Step-by-step solution

  1. Log A.
  2. Schedule the timer.
  3. Log C.
  4. Then timer fires: B.

Why the other options are wrong

  • A. Timer callbacks wait for the call stack to empty.
  • B. Nothing runs before the synchronous code.
  • C. Synchronous logs run in written order.

Question 4Easy

Which JavaScript construct lets a long-running task yield so that other work can proceed on the single main thread?

  1. Declaring the function with the var keyword
  2. Splitting it into asynchronous chunks that return control to the event loop
  3. Wrapping the loop in a try block
  4. Adding more console.log calls

Answer: B. Splitting it into asynchronous chunks that return control to the event loop

Explanation

JavaScript runs on one thread; breaking work into async chunks (timers, promises) lets the event loop handle other events between them.

Step-by-step solution

  1. The main thread runs one task at a time.
  2. Async chunks give control back to the loop.

Why the other options are wrong

  • A. Declaration style has no effect on blocking.
  • C. try/catch handles errors, it does not yield.
  • D. Logging does not yield control.

Question 5Medium

What is the output?

console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => console.log(3)).then(() => console.log(4));
queueMicrotask(() => console.log(5));
console.log(6);
  1. 1, 6, 2, 3, 5, 4
  2. 1, 6, 3, 4, 5, 2
  3. 1, 6, 5, 3, 4, 2
  4. 1, 6, 3, 5, 4, 2

Answer: D. 1, 6, 3, 5, 4, 2

Explanation

After 1 and 6, microtasks run in queue order: 3, 5, then the chained 4; the timer 2 runs last.

Step-by-step solution

  1. Sync: 1, 6.
  2. Microtask queue: [3, 5].
  3. Running 3 queues 4 after 5.
  4. Timer last: 2.

Why the other options are wrong

  • A. Microtasks run before timers.
  • B. The second then is queued after the queueMicrotask callback.
  • C. Microtasks run in the order queued: 3 before 5.

Question 6Medium

What is the output?

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}
  1. 3, 3, 3
  2. 0, 1, 2
  3. 2, 2, 2
  4. undefined, undefined, undefined

Answer: A. 3, 3, 3

Explanation

The callbacks run after the loop ends; they share one var i which is 3 by then.

Step-by-step solution

  1. Loop finishes with i = 3.
  2. Three timers fire later.
  3. Each prints 3.

Why the other options are wrong

  • B. The callbacks run after the loop, when i is 3.
  • C. The loop exits with i equal to 3.
  • D. var i is still visible to the callbacks.

Question 7Medium

A promise callback and a setTimeout(fn, 0) callback are both ready. Which runs first and why?

  1. The setTimeout callback, because timers are always checked first
  2. The promise callback, because the microtask queue is drained before the next macrotask
  3. Whichever was registered first, since both share one queue
  4. They run in parallel on separate threads

Answer: B. The promise callback, because the microtask queue is drained before the next macrotask

Explanation

Microtasks (promise reactions) run to completion after the current task, before the next macrotask such as a timer.

Step-by-step solution

  1. Current task ends.
  2. Microtasks drain.
  3. Next macrotask runs.

Why the other options are wrong

  • A. Timers are macrotasks and wait for microtasks.
  • C. They use two different queues.
  • D. Both run on the single JS thread.

Question 8Medium

What is logged, in order?

console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => console.log(3));
console.log(4);
  1. 1, 4, 3, 2
  2. 1, 3, 4, 2
  3. 1, 4, 2, 3
  4. 1, 2, 3, 4

Answer: A. 1, 4, 3, 2

Explanation

Synchronous code runs first (1, 4). Then the microtask queue (promise callback: 3) is drained before the next macrotask (timer: 2).

Step-by-step solution

  1. console.log(1) and console.log(4) run immediately as part of the main synchronous code, logging 1 then 4 right away.
  2. setTimeout schedules its callback as a macrotask (to run later), while Promise.resolve().then() schedules its callback as a microtask.
  3. After all synchronous code finishes, the JavaScript engine drains the ENTIRE microtask queue first (logging 3), and only THEN moves on to the next macrotask (the timer, logging 2).

Why the other options are wrong

  • B. "1, 3, 4, 2" would require the microtask (3) to run interleaved with synchronous code, but ALL synchronous code (including console.log(4)) finishes first, before any microtask runs.
  • C. "1, 4, 2, 3" reverses the actual order between the timer and the microtask; microtasks always run before the next macrotask, so 3 comes before 2, not after.
  • D. "1, 2, 3, 4" would require the timer callback to run before even the synchronous console.log(4), but synchronous code always finishes completely before any queued task (microtask or macrotask) begins.

Question 9Hard

What is the output?

async function a() { console.log('a1'); await b(); console.log('a2'); }
async function b() { console.log('b'); }
a();
Promise.resolve().then(() => console.log('p'));
console.log('end');
  1. a1, b, end, p, a2
  2. a1, b, a2, end, p
  3. a1, end, b, a2, p
  4. a1, b, end, a2, p

Answer: D. a1, b, end, a2, p

Explanation

a1 and b run synchronously, end follows; microtasks then run in order: the continuation a2 was queued first, then p.

Step-by-step solution

  1. a1 and b print synchronously.
  2. await queues a2 as a microtask.
  3. p is queued next.
  4. end prints; then a2, p.

Why the other options are wrong

  • A. a2 is queued before p.
  • B. await always yields to the caller.
  • C. b() runs synchronously when called.

Question 10Hard

What is logged, in order?

setTimeout(() => console.log("A"), 0);
Promise.resolve()
  .then(() => {
    console.log("B");
    setTimeout(() => console.log("C"), 0);
  })
  .then(() => console.log("D"));
console.log("E");
  1. E, B, A, D, C
  2. E, B, D, A, C
  3. E, A, B, C, D
  4. B, D, E, A, C

Answer: B. E, B, D, A, C

Explanation

E is synchronous. The microtasks B and then D run next. The timers run afterwards in the order they were scheduled: A first, then C (added during B).

Step-by-step solution

  1. console.log("E") runs first, as part of the main synchronous code.
  2. Then the microtask queue runs completely before any macrotask: the first .then() logs "B" and schedules a NEW setTimeout (for "C") while running; the second .then() then logs "D", since it's chained and also a microtask.
  3. Finally, the macrotask (timer) queue runs in the order timers were scheduled: "A" was scheduled first (right at the top, before anything else ran), so it logs before "C" (which was only scheduled later, during the "B" microtask).

Why the other options are wrong

  • A. "E, B, A, D, C" wrongly places the timer "A" before the second microtask "D"; ALL pending microtasks (including the chained "D") run to completion before ANY macrotask (timer) is processed.
  • C. "E, A, B, C, D" wrongly runs the timers before the microtask chain even starts; microtasks always get priority over macrotasks, so "B" and "D" must both finish before "A" can run.
  • D. "B, D, E, A, C" wrongly puts microtask callbacks ("B", "D") before the synchronous code ("E"); all synchronous code always finishes completely before the event loop even looks at the microtask queue.

Tip: When a microtask callback itself schedules a NEW timer, that new timer still has to wait behind any OLDER timers that were scheduled earlier, and behind the entire current microtask queue.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all JavaScript topics · All topics