Autocoder

JavaScript Event Loop Interview Questions

Last updated 2026-11-19

The event loop explains how a single-threaded language handles timers, network calls and user input without freezing. Interviewers use it to test depth: can you place a log statement in the right order among timers, promises and synchronous code, and can you explain why a long loop freezes a page?

The questions here build up from the call stack and the queues, through the microtask and macrotask distinction, to Node specifics such as process.nextTick and setImmediate. Each output prediction lists the exact order.

Level1 to 5 years
FocusCall stack, task queues, ordering
RuntimeBrowser and Node.js

The mental model

Keep five things in mind: the call stack, the macrotask queue (timers, I/O, UI events), the microtask queue (promise reactions, queueMicrotask, MutationObserver), the rule that the stack must be empty before anything is dequeued, and the rule that the whole microtask queue drains before the next macrotask.

Related topics

Promise behaviour is covered in promises and async/await, and the capture rules for callbacks in closures. The preparation plan is in how to crack a JavaScript interview.

JavaScript Event Loop Interview Questions: questions with worked answers

  1. 1

    What is the event loop?

    easy

    The event loop is the mechanism that lets JavaScript, which executes on one thread, do non-blocking work. The runtime hands slow operations such as timers and network requests to the host environment. When they finish, their callbacks are placed in queues. The loop repeatedly checks whether the call stack is empty and, if so, takes the next task from the queues and pushes it onto the stack.

  2. 2

    What is the call stack?

    easy

    The call stack records the functions currently executing. Calling a function pushes a frame, and returning pops it. Only the function at the top runs, which is why JavaScript is described as single-threaded. Infinite recursion fills the stack and throws a RangeError for exceeding the maximum call stack size.

  3. 3

    What is the difference between the microtask queue and the macrotask queue?

    medium

    Macrotasks, also called tasks, include setTimeout and setInterval callbacks, I/O, UI events and setImmediate in Node. Microtasks include promise then, catch and finally handlers, queueMicrotask callbacks and the continuation after await. After each macrotask, and whenever the stack empties, the engine runs all microtasks until the queue is empty, then continues to the next macrotask. So microtasks always run before the next timer.

  4. 4

    Predict the output: timers, promises and sync code.

    hard

    Synchronous logs run first: start and end. Then the microtask: promise. Finally the timer: timeout, even though its delay is zero. The delay is a minimum, and the callback must wait for the stack and microtasks to clear.

    javascript
    console.log("start");
    setTimeout(() => console.log("timeout"), 0);
    Promise.resolve().then(() => console.log("promise"));
    console.log("end");
    // start end promise timeout
  5. 5

    Predict the output: nested microtasks and timers.

    hard

    After the synchronous code, the microtask queue drains completely, including microtasks added during the drain, before any timer runs. The timers run in the order of their delay and registration.

    javascript
    setTimeout(() => console.log("t1"), 0);
    Promise.resolve().then(() => {
      console.log("p1");
      Promise.resolve().then(() => console.log("p2"));
    });
    setTimeout(() => console.log("t2"), 0);
    console.log("sync");
    // sync p1 p2 t1 t2
  6. 6

    Predict the output: setTimeout with different delays and a blocking loop.

    hard

    Timers register their callbacks after the delay, but the callback can only run when the stack is free. The busy loop holds the thread for 200 ms, so both callbacks run late, in order of their due time: the 0 ms timer first, then the 10 ms one. The measured gap shows the delay is a minimum.

    javascript
    const t0 = Date.now();
    setTimeout(() => console.log("b", Date.now() - t0 >= 200), 10);
    setTimeout(() => console.log("a", Date.now() - t0 >= 200), 0);
    while (Date.now() - t0 < 200) {}
    console.log("loop done");
    // loop done, a true, b true
  7. 7

    Why does a long-running loop freeze the page, and how do you avoid it?

    medium

    The browser renders and handles input only between tasks. A long synchronous loop keeps the main thread busy, so nothing paints or responds. Split the work into chunks scheduled with setTimeout or requestIdleCallback, move heavy computation to a Web Worker, or use asynchronous APIs. Yielding after each chunk lets the loop process events in between.

    javascript
    function processChunked(items, fn, size = 1000) {
      let i = 0;
      function step() {
        const end = Math.min(i + size, items.length);
        for (; i < end; i++) fn(items[i]);
        if (i < items.length) setTimeout(step, 0);
      }
      step();
    }
  8. 8

    Is setTimeout(fn, 0) really immediate?

    medium

    No. It queues fn as a macrotask after at least the given delay, and browsers clamp nested timers to a minimum of about 4 ms after several levels. The callback runs only after the current task and all microtasks finish. Use queueMicrotask or a resolved promise if you need to run something right after the current code but before other tasks.

  9. 9

    What are process.nextTick and setImmediate in Node.js?

    hard

    process.nextTick queues a callback that runs after the current operation, before the event loop continues and before promise microtasks, so it can starve I/O if used recursively. setImmediate runs its callback in the check phase, after the poll phase. From inside an I/O callback, setImmediate always fires before a zero-delay timer, but from the main module the order of setTimeout 0 and setImmediate is not guaranteed.

    javascript
    Promise.resolve().then(() => console.log("promise"));
    process.nextTick(() => console.log("nextTick"));
    console.log("sync");
    // sync nextTick promise
  10. 10

    What are the phases of the Node.js event loop?

    medium

    In order: timers (due setTimeout and setInterval callbacks), pending callbacks, idle and prepare, poll (retrieve new I/O events and run their callbacks), check (setImmediate callbacks) and close callbacks. Between each phase, nextTick and microtask queues are drained. This is why Node can handle many connections with one thread, handing file and network work to the operating system and a thread pool.

  11. 11

    How does requestAnimationFrame fit in?

    medium

    In browsers, requestAnimationFrame callbacks run just before the next repaint, typically 60 times a second, and are the right tool for visual updates. They are neither microtasks nor ordinary timers: the browser runs them in the rendering step of the event loop, after microtasks and before painting.

  12. 12

    How can a JavaScript program do work in parallel?

    medium

    The main thread is single, but Web Workers in browsers and worker_threads in Node run scripts on separate threads with their own event loops. They communicate by postMessage, copying data with structured clone or transferring ownership of buffers, and share memory only through SharedArrayBuffer. Use them for CPU-heavy tasks such as parsing, image processing and cryptography.

  13. 13

    Predict the output: async function interleaving.

    hard

    Each await yields to the microtask queue. After the synchronous parts, the continuation of the two functions interleave in order of their awaits.

    javascript
    async function one() { console.log("1a"); await null; console.log("1b"); }
    async function two() { console.log("2a"); await null; console.log("2b"); }
    one(); two(); console.log("main");
    // 1a 2a main 1b 2b
  14. 14

    What happens if a microtask keeps scheduling more microtasks?

    medium

    The microtask queue is drained until empty before the loop moves on, so a microtask that always schedules another starves timers, I/O and rendering, and the page or process hangs even though no single call is long. The same applies to a recursive process.nextTick in Node. Use setTimeout or setImmediate to give other work a turn.

Frequently asked questions

Is JavaScript synchronous or asynchronous?

The language executes synchronously on one thread. Asynchronous behaviour comes from the environment, which schedules callbacks through the event loop.

Which runs first, a promise callback or setTimeout?

The promise callback, because microtasks drain before the next macrotask, even when the timer delay is zero.

Does async/await block the thread?

No. Await suspends only the current function. The thread continues to run other tasks, and the function resumes as a microtask when the awaited promise settles.

Where does the event loop live?

It is part of the host environment, the browser or Node.js, not of the ECMAScript engine. The two implement it differently in details, such as phases and extra queues.

Related guides

More in Programming Language Interview Questions by Language

See all in this section