CodeZettaInterview Hub

JavaScript · Event Loop · Mid-Level · Code Output

Predict the output order: ```js console.log('A'); setTimeout(() => console.log('B'), 0); Promise.resolve().then(() => { console.log('C'); setTimeout(() => console.log('D'), 0); }); console.log('E'); ```

Short Interview Answer

`A`, `E`, `C`, `B`, `D`.

Detailed Explanation

Synchronous logs: A then E. The promise microtask runs next: C, and schedules timeout D. Then the timer task queue runs B (scheduled first), then D. All microtasks from the script complete before any timer callbacks. This tests understanding of sync → microtasks → macrotasks and that microtasks can enqueue more macrotasks.

Example

// Output:
// A
// E
// C
// B
// D

Interview Tip

Narrate phases; don't just list letters.

Common Mistake

Putting B before C.

Did you know this answer?