Event Loop

Mechanism that handles asynchronous operations and ensures that long-running tasks don't freeze the program. Enables responsive user interfaces and efficient handling of I/O operations.

How it works

  • JavaScript runs code in a single thread.
  • Synchronous tasks execute immediately on the call stack.
  • Asynchronous tasks (like timers or API calls) are sent to a separate queue.
  • When the call stack is empty, the event loop checks the queue.
  • If there are tasks in the queue, it moves them to the call stack for execution.
  • This process repeats, allowing JavaScript to handle multiple operations without blocking.