Javascript – setTimeout(function(){console.log(3)}, 0); it has value 0


When I execute the below code in the console I am getting as 1,4, undefined 3,2.

I would like to know why its not executing as 1,3,4 and 2
since in setTimeout(function(){console.log(3)}, 0); the milliseconds param is 0.

    (function() {
        setTimeout(function(){console.log(2)}, 1000); 
        setTimeout(function(){console.log(3)}, 0); 

Best Solution

Here's a great explanation by John Resig:
But the bottom line is console.log(1) and (4) are executed 'in-line' and 2 and 3 are placed in the event queue, and don't get executed until the all of the in-line code is executed. So, even though the delay is 0 for (3), it still occurs after all statements are executed.
I'm not getting the undefined message when I test you code either.