Programming Languages JavaScript Subjective
Sep 25, 2025

What is the Event Loop in JavaScript?

Detailed Explanation

The Event Loop is the mechanism that handles asynchronous operations in JavaScript's single-threaded environment.

Components:
1. Call Stack - executes functions (LIFO)
2. Web APIs - browser APIs (setTimeout, DOM events, etc.)
3. Callback Queue - completed async operations (FIFO)
4. Event Loop - moves callbacks from queue to stack

Process:


console.log("1");
setTimeout(() => console.log("2"), 0);
console.log("3");
// Output: 1, 3, 2

Execution Order:
1. Synchronous code executes first
2. Async operations go to Web APIs
3. Completed operations go to Callback Queue
4. Event Loop moves callbacks to Call Stack when empty

Microtasks vs Macrotasks:
- Microtasks (higher priority): Promises, queueMicrotask
- Macrotasks: setTimeout, setInterval, DOM events

Microtasks execute before macrotasks in each loop iteration.

Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback