What is event loop in javascript

While preparing for JavaScript interviews, I see topics like the event loop, call stack, and task queues.
I understand JavaScript is single-threaded, but I struggle to connect these concepts practically.
What is the event loop, and why is it important?

This confusion is very normal—once the event loop clicks, JavaScript suddenly makes a lot more sense. Let’s break it down practically, not theoretically.


:one: First: What does “JavaScript is single-threaded” mean?

JavaScript can do only one thing at a time.

It has:

  • Call Stack → where functions are executed

  • Only one call stack → so no parallel execution of JS code

So the big question is:

:red_question_mark: If JS is single-threaded, how does it handle timers, API calls, clicks, etc., without blocking?

:backhand_index_pointing_right: Answer: The Event Loop


:two: The Main Components (Think of them as a team)

:small_blue_diamond: Call Stack (The Worker)

  • Executes JS code line by line

  • Functions are pushed when called and popped when finished

function a() {
  console.log("A");
}
a();

Call Stack:

a()


:small_blue_diamond: Web APIs (The Assistants)

Provided by the browser (not JS itself):

  • setTimeout

  • DOM events

  • fetch

  • Promises

They handle time-consuming tasks in the background.


:small_blue_diamond: Task Queues

Where completed async tasks wait.

There are two important queues:

:one: Macro Task Queue (Callback Queue)

  • setTimeout

  • setInterval

  • DOM events

:two: Micro Task Queue (Higher priority)

  • Promise.then

  • MutationObserver


:three: What is the Event Loop? :cyclone:

The event loop is a watchdog that constantly checks:

:face_with_monocle: “Is the call stack empty?”

  • If YES → push tasks from queues into the call stack

  • Microtasks first, then macrotasks

:pushpin: This is why promises execute before setTimeout


:four: Let’s See This in Action (Very Important Example)

console.log("Start");

setTimeout(() => {
  console.log("Timeout");
}, 0);

Promise.resolve().then(() => {
  console.log("Promise");
});

console.log("End");

:magnifying_glass_tilted_left: Step-by-step Execution

:one: Synchronous code (Call Stack):

Start
End

:two: Async handling:

  • setTimeout → Web API → Macro task queue

  • Promise.then → Micro task queue

:three: Event loop checks stack (empty):

  • Executes microtasks firstPromise

  • Then macrotasks → Timeout

:white_check_mark: Output:

Start
End
Promise
Timeout


:five: Why Is the Event Loop So Important?

:fire: Without the event loop:

  • JS would freeze during API calls

  • UI would become unresponsive

  • No async programming

:fire: With the event loop:

  • Non-blocking execution

  • Smooth UI

  • High-performance apps

This is why JavaScript can power browsers, Node.js, and servers.


:six: Real-World Analogy (Easy to Remember)

:convenience_store: Restaurant example

  • Call Stack → Chef (single)

  • Web APIs → Assistants cooking

  • Queues → Orders ready

  • Event Loop → Manager deciding which order the chef cooks next

Chef always finishes current dish before starting another.


:seven: Interview-Ready One-Line Answer :white_check_mark:

The event loop is a mechanism that allows JavaScript to handle asynchronous operations by continuously checking the call stack and pushing queued callbacks into it once the stack is empty, ensuring non-blocking execution.


:eight: What Interviewers REALLY Test

  • Order of execution

  • Microtask vs macrotask priority

  • Understanding async behavior

1 Like

JavaScript is single-threaded, meaning it can do only one thing at a time. The event loop is what allows JavaScript to handle asynchronous tasks like setTimeout, promises, and API calls without blocking the main thread.

In simple terms:

  • The call stack runs synchronous code

  • Async tasks go to task queues / microtask queues

  • The event loop keeps checking if the call stack is empty and pushes queued tasks back for execution

This is important because it enables non-blocking behavior, keeping web apps fast and responsive — a key concept often tested in JavaScript interviews.
Real-world analogy

Think of JavaScript like a single cashier at a store:

  • The cashier (call stack) handles customers one by one

  • Long tasks (API calls, timers) are sent aside

  • When the cashier is free, the event loop brings the next waiting customer back


JavaScript example

console.log("Start");

setTimeout(() => {
  console.log("Timeout");
}, 0);

Promise.resolve().then(() => {
  console.log("Promise");
});

console.log("End");

Output

Start
End
Promise
Timeout

Why this happens

  • Start and End run first (call stack)

  • Promise goes to the microtask queue (higher priority)

  • setTimeout goes to the task queue

  • The event loop executes microtasks before tasks

1 Like

The event loop is a core concept in JavaScript that allows it to handle asynchronous operations even though it runs on a single thread. It manages how tasks are executed without blocking the main execution flow.

In simple terms, JavaScript executes code using a call stack. When asynchronous operations like setTimeout, promises, or API calls are encountered, they are handled by the browser (or Node.js) and placed into task queues. The event loop continuously checks whether the call stack is empty and, if so, pushes tasks from the queue into the stack for execution.

There are different queues involved, such as the microtask queue (for promises and async/await) and the callback/task queue (for timers and events). Microtasks are always executed before regular callbacks.

Understanding the event loop is important because it explains why certain asynchronous code runs before others and helps developers write non-blocking, efficient JavaScript applications.

1 Like