The Event Loop and Queue
The Event Queue
The Event Queue is a queue that keeps track of tasks that are waiting to be put on the call stack to be executed. Tasks get added to the Event Queue by Web APIs that run in parallel with the JavaScript run time.
Here are three examples of Web APIs that add tasks to the event queue:
Timers - Timers schedule tasks to be added to the event queue
DOM Event Handlers - User Interactions such as mouse clicks and keyboard presses are handled by putting tasks on the event queue
Network Requests - Network requests are processed asynchronously and send back results by putting tasks on the event queue
The Event Loop
When the call stack is empty, it takes the first task off the event queue and processes it. The remaining tasks on the queue wait until the call stack is empty again. This cycle is called the Event Loop.