← All tutorials

GoCareerGo Tutorials

JavaScript Tutorial

Variables to closures, promises, the DOM and OOP — a complete JS reference.

The Event Loop

The mechanism that lets single-threaded JavaScript handle async operations without blocking.

  • Call stack — runs synchronous code, one frame at a time
  • Web APIs / Node APIs — handle timers, I/O and network calls in the background
  • Microtask queue — Promise callbacks (.then, async/await) — runs before macrotasks
  • Macrotask (callback) queue — setTimeout, setInterval, I/O callbacks
  • The event loop keeps pulling from the microtask queue, then the macrotask queue, whenever the call stack is empty
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');

// Output: 1, 4, 3, 2
// Microtasks (Promises) always run before macrotasks (setTimeout)
Tip

That exact '1, 4, 3, 2' example is one of the most common JS interview trick questions — memorize the reasoning, not just the answer.