Back to Developer Roadmap

Event Loop Js

src/data/question-groups/frontend/content/event-loop-js.md

4.0942 B
Original Source

The event loop is a core concept in JavaScript, and it allows for the execution of asynchronous code.

The way it works, is as follows:

  1. Call Stack: JavaScript executes your code on a single thread using a call stack, where function calls are added and executed one by one. When a function ends, it's removed from the stack.
  2. Async calls: For asynchronous operations, JavaScript uses Web APIs provided by the browser. These operations are offloaded from the call stack and handled separately.
  3. Tasks Queue: Once an asynchronous call is done, its callback is placed in the task queue.
  4. Event Loop: The event loop constantly checks the call stack and the task queue. If the call stack is empty, it takes the first task from the queue and pushes it onto the call stack for execution. This cycle repeats indefinitely.