0

Why is the Output first, third and second??

console.log("first");
setTimeout(() => {
  console.log('second');
}, 0);
console.log('third');
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • 1
    because `second` is coming from an async process – Mister Jojo Jul 25 '21 at 17:28
  • 1
    to add @MisterJojo, you can see [this video](https://youtu.be/8aGhZQkoFbQ?t=770) it explains the javascript event loop and everything related to it. – Syakhisk Al-azmi Jul 25 '21 at 17:30
  • 1
    Because `setTimeout` callback is put in a queue. Queued jobs will only execute when the current script has finished executing, and the call stack is empty. – trincot Jul 25 '21 at 17:34

0 Answers0