#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read
•Question 3 of 50hard

What is the Event Loop in JavaScript?

Understanding asynchronous JavaScript and the event loop.

What You'll Learn

  • How the event loop works
  • Microtasks vs macrotasks
  • Execution order

What is the Event Loop?

The event loop allows JavaScript to perform non-blocking operations despite being single-threaded.

How It Works

  1. Execute synchronous code in the call stack
  2. Check microtask queue (Promises)
  3. Check macrotask queue (setTimeout, setInterval)
  4. Repeat

Example

code.jsJavaScript
console.log('1');

setTimeout(() => console.log('2'), 0);

Promise.resolve().then(() => console.log('3'));

console.log('4');

// Output: 1, 4, 3, 2

Why This Order?

  • 1, 4: Synchronous code runs first
  • 3: Microtasks (Promises) run next
  • 2: Macrotasks (setTimeout) run last