Start here.
JavaScript often starts work that will finish later, such as a timer, a network request, or a click handler. The current code does not need to wait idly for each event. Instead, callbacks are scheduled to run when the current work has finished and their turn arrives. The event loop is the coordination model that takes queued work and runs it one piece at a time on the main JavaScript thread.
Synchronous statements run in order on the call stack. Calling a function adds its work to that stack; returning removes it. setTimeout(callback, 0) schedules a callback for a later task, even with zero delay. Promise reactions are queued as microtasks, which are processed after the current stack is empty and before the next ordinary task. This is why a promise callback can appear before a zero-delay timer.
The event loop does not make CPU-heavy code run in parallel on the same thread. A long synchronous loop blocks the browser from handling events and painting updates until it finishes. Asynchronous APIs let the environment manage waiting and later schedule continuation. When reading output order, separate what runs now from what is merely registered to run later, then ask which queue receives each continuation.
Build the model.
JavaScript runs one piece of ordinary code at a time on a given thread. The host can wait for timers, input, or network activity and later queue callbacks. An event loop takes queued work when the current call stack is clear.
Promise reactions run as microtasks, which are processed before the next ordinary task such as a timer callback. This ordering explains surprising logs without requiring that JavaScript execute two statements simultaneously. Long synchronous work still blocks the page from responding until it finishes.
How it works.
Synchronous statements run to completion on the current call stack. setTimeout asks the host to schedule a callback after at least a delay; it does not insert the callback into the middle of the current stack. Promise reactions enter a microtask queue. After the current synchronous work ends, the event loop processes microtasks before taking the next ordinary task such as the timer callback.
This model helps you understand both ordering and responsiveness. A zero-millisecond timeout is not immediate. A long calculation or infinite loop can keep the call stack busy so a button event cannot be handled. Break heavy work into smaller tasks or use a worker when the page needs to stay responsive. The browser can perform external work while JavaScript waits, but your synchronous loop still occupies its turn.
Read it line by line.
console.log('A');
setTimeout(() => console.log('C'), 0);
Promise.resolve().then(() => console.log('B'));First notice. The output is A, B, C: synchronous work, then a microtask, then a timer task.
A logs immediately. setTimeout registers a timer callback but does not call it now. Promise.resolve().then registers a microtask. When the current script finishes, the microtask logs B. Only after that does the event loop take the timer task and log C. The order is A, B, C even with a zero delay.
See the rule again.
console.log('one');
queueMicrotask(() => console.log('three'));
console.log('two');The output is one, two, three. queueMicrotask schedules work for after the current stack. The second synchronous console.log still runs first. This makes the scheduling boundary visible without a timer.
One thing to remember.
A zero-delay timer waits for a future turn; it does not interrupt this one.