It is a mechanism that allows JavaScript to perform non-blocking operations despite being single-threaded. It manages the execution of multiple chunks of your script over time.
How it works:
• Call Stack: Executes your synchronous code line by line.
• Web APIs: Handles async tasks (like timers or fetches) in the background.
• Task Queue: Holds the callbacks of finished async tasks.
• The Loop: It constantly checks if the Call Stack is empty. If empty, it picks the first task from the Queue and pushes it to the Stack to run.
Problem solved:
It prevents "blocking." Without it, a slow task would freeze the entire program, making it unresponsive until that task finishes.
Use case:
When calling
setTimeout(), the timer runs in the background. The Event Loop ensures your main code keeps running and only executes the timer's callback once the stack is clear.