JavaScriptInterview preparation
Intermediate·Output prediction·2 min read

What is the output here?

Practice this question

Trace the code before revealing the explanation. Describe the output and the reason for its order.

Definition:

Output prediction requires following the order in which synchronous code and queued callbacks can execute. This example combines the current script, a promise microtask and a timer callback. A zero-delay timer becomes eligible later; it does not interrupt the running script.

Example and explanation:

JavaScript
console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
console.log('D');
  • Output: A D C B.

  • Synchronous logs run first, then the promise callback, then the timer.

  • The promise is already fulfilled, but its .then callback is still queued.

  • The example's ordering is predictable; more complex Node timer/I/O ordering may depend on where callbacks were scheduled.

Interview point:

  • Explain why each log runs at that point instead of memorizing a sequence of letters.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “What is the output here?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.