Event Loop in JavaScript

  • JavaScript is single-threaded.
  • The event loop handles asynchronous tasks (like promises, timers) by moving them from the callback queue to the main execution stack when it’s free.

Example:

console.log("Start");
setTimeout(() => console.log("Callback"), 0);
console.log("End");
// Output: Start → End → Callback

Even with 0ms, the async callback runs after synchronous code.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *