This is a reminder guide for me to how and what to know about the weird things in JS. Get to basics first.
-
What are Event loops in Javascript? Refs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop https://www.youtube.com/watch?v=8aGhZQkoFbQ
-
What will be output of this:
console.log(1)
setTimeout(function(){ console.log(2) })
console.log(3)A: 1,3,2.
Why? Well because
setTimeoutwith 0 interval doesn't mean it will execute after 0 milliseconds. the setTimeout needs to wait for all the codes to complete even though you specified a particular time limit for your setTimeout Basically, this means setTimeout will wait for event Queue to be empty. Simply put, setTimeout() re-queues the new JavaScript at the end of the execution queue