Curriculum / Advanced / Lesson 27

Event Loop

How JavaScript handles concurrency with the event loop.

27 of 30
advanced 📖 7 min read 🎯 4 questions

What is the Event Loop?

The event loop is what allows JavaScript to handle asynchronous operations despite being single-threaded. It's the mechanism that coordinates the call stack, task queue, and microtask queue.

💡 Key insight JavaScript is single-threaded but non-blocking. The event loop manages this by processing tasks from the task queue when the call stack is empty.

Key Components

Execution Order

  1. Synchronous code executes immediately
  2. Microtasks (Promises) execute before tasks
  3. Tasks (setTimeout, events) execute
  4. Browser rendering happens between tasks

Common Mistakes

Quick Quiz

1. What is the event loop?

2. Which has higher priority: microtasks or macrotasks?

3. What happens when the call stack is empty?

4. Which is a macrotask?

Summary

Back to curriculum