JavaScript Interview Question #74

What is the JavaScript event loop?

DOM, Events & Event Loop Fundamentals Mid-Level Intermediate

Quick Interview Answer

The event loop coordinates execution between the JavaScript call stack and work scheduled by the runtime. When the call stack becomes available, queued work can be selected for execution according to the runtime's scheduling rules.

Detailed Explanation

The event loop coordinates execution between the JavaScript call stack and work scheduled by the runtime. When the call stack becomes available, queued work can be selected for execution according to the runtime's scheduling rules.

Promise reactions use the microtask queue, while timers and many other callbacks are processed as tasks. After the current synchronous job finishes, pending microtasks are processed before the runtime proceeds to the next task.

Code Example

console.log("Start");

setTimeout(() => console.log("Timer"), 0);

Promise.resolve()
    .then(() => console.log("Promise"));

console.log("End");

// Start
// End
// Promise
// Timer