DEV Community

Cover image for The best visual explanation (probably) of the Event Loop in JavaScript.
HRomium
HRomium

Posted on • Originally published at hromium.com

The best visual explanation (probably) of the Event Loop in JavaScript.

In this article, you'll get a clear, visual explanation of how the Event Loop works — using analogies, step-by-step examples, and real code.

👉 A fully interactive Event Loop model is available at:

https://hromium.com/javascript-visualized-event-loop

Use it to explore different scenarios, test your assumptions, and truly understand how JavaScript executes code under the hood.

Let’s dive in.

The Visual Model Behind This Explanation

Let’s start with a basic code snippet that I’ll use to walk you through how the Event Loop works step by step:

console.log('Start');

setTimeout(() => {
    console.log('setTimeout (Macro task)');
}, 0);

Promise.resolve().then(() => {
    console.log('Promise.then (Micro task)');
});

queueMicrotask(() => {
    console.log('queueMicrotask (Micro task)');
});

console.log('End');

Enter fullscreen mode Exit fullscreen mode

In this example, we have:

  • Synchronous code: lines 1 and 15
  • One macrotask: the setTimeout on line 3
  • Two microtasks: Promise.then on line 7 and queueMicrotask on line 11

To make this process visual and intuitive, I use a simple representation:

  • The macrotask is shown as a large red ball
  • The microtasks are two smaller green balls
  • The synchronous code appears as white cubes

EventLoop code snippet

This setup makes it easy to follow how tasks move through the system — from being scheduled to being executed.

The code is executed in the main thread of the program, moving from top to bottom, line by line.
Every operation is placed into the Call Stack.

CallStack

From there, tasks either get passed to the Web API for handling or immediately produce output visible to the user.

Prefer video?

If you find video content easier to follow, this explanation is also available on YouTube.

Tasks that return from the Web API after being processed are placed into separate queues:

  • Microtasks go into a queue called the MicroTask Queue
  • Macrotasks go into the Task Queue

Task Queue & Microtask Queue

When the Call Stack becomes empty, a mechanism called the Event Loop takes over.

In the visual model, it is represented as a yellow, egg-shaped mechanism.

The Event Loop first opens the MicroTask Queue and pushes all microtasks from it into the Call Stack, one by one, until the queue is empty.

Only after all microtasks have been completed does the Event Loop turn to the Task Queue, allowing one macrotask to enter the Call Stack per cycle.

EventLoop Visualized

Explaining the Event Loop in Action

Now it's time to explain how the Event Loop works in JavaScript using the simple example from earlier.

The execution begins with the synchronous code.

The console.log('Start') statement on the first line enters the Call Stack and is executed immediately.

As a result, the message Start appears on the screen.

synchronous code

Next, the setTimeout call is encountered on line 3.

Macrotask

Although the delay is set to zero, this function is not executed immediately.

Instead, it is scheduled as a macrotask and sent to the Web API, which is responsible for tracking the timer duration.

Once the timer completes, the macrotask is moved to the Task Queue, where it waits to be picked up by the Event Loop.

Task Queue

After that, two microtasks are scheduled in sequence:

One using Promise.then, and the other using queueMicrotask.

Microtasks execution

Both of these are also passed through the Web API, and once prepared, they are placed into the MicroTask Queue

Microtask Queue

After scheduling the asynchronous operations, the synchronous code continues to execute.

The final console.log('End') is run, and we see the message End appear on the screen.

synchronous code finished

At this point, the Call Stack is empty, and it's time for the Event Loop to take over.

The Event Loop first processes the MicroTask Queue:

Event Loop first processes the MicroTask Queue

  • The Promise.then microtask is executed, printing Promise.then (Micro task)
  • Then, the queueMicrotask is executed, printing queueMicrotask (Micro task)

printing on screen

Only after all microtasks have been completed does the Event Loop move to the Task Queue.

Event Loop move to the Task Queue

There, the previously scheduled setTimeout macrotask is executed, printing setTimeout (Macro task).

printing setTimeout (Macro task)

This is how the Event Loop controls the order of execution:

first synchronous code, then microtasks, and only after that, macrotasks.

Now you have a clear understanding of how the Event Loop works in JavaScript — just in case it comes up in an interview.

In the following parts of this series, I’ll be covering topics like:

  • How the Web API works behind the scenes
  • JavaScript memory management and the HEAP
  • Specifics of the Event Loop in Node.js
  • And much more — all explained in a simple and accessible way

And don’t forget: the interactive Event Loop model featured in this article is available on the website.

You can experiment with it, test different scenarios, and truly understand how JavaScript handles task execution:
https://hromium.com/javascript-visualized-event-loop

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay