Understanding the JavaScript Event Loop: A Comprehensive Guide
Introduction
JavaScript is a single-threaded, non-blocking, asynchronous, concurrent language.
It has a heap, call stack, an event loop, a callback queue, and some other APIs.
If this sounds confusing, don’t worry.
In this article, we’ll break down these concepts and explore the JavaScript Event Loop in detail.
What is the Event Loop?
The Event Loop is a process in the JavaScript runtime that takes care of executing the code, collecting and processing events, and executing queued sub-tasks.
This process is what allows JavaScript to perform non-blocking I/O operations, despite being single-threaded.
How does the Event Loop work?
The Event Loop operates on a single thread, with the help of a few data structures, it can give us the illusion of multi-threading.
Here’s a step-by-step breakdown of how it works:
1. Heap
Objects are allocated in a heap which is just a name to denote a large (mostly unstructured) region of memory.