Event loop

The event loop is the mechanism that lets JavaScript, which runs its code on a single thread, handle asynchronous operations without blocking. Deferred tasks wait in queues, and the loop runs them when the call stack is empty, processing microtasks (promises) first.

Example

console.log(1)
setTimeout(() => console.log(2), 0)
console.log(3)  // prints 1, 3, 2

Related concepts

The whole glossary