0

I have below code

console.log('one'); 
setTimeout(function() { console.log('two'); });
console.log('three');

The output of code when I run it is one, three, two

However I felt it should have been one, two, three as I haven't provided the wait time , which will set the wait time to 0 as default, i.e no waiting.

Then what is the reason that the output is one, three, two and not one, two, three.

Chetan
  • 4,735
  • 8
  • 48
  • 57

1 Answers1

2

setTimeout schedules the function for execution. That scheduler doesn't do its thing until after the current thread yields control back to the browser, e.g. after the last logging statement has executed.