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

functionjavascript

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() {
        console.log(1); 
        setTimeout(function(){console.log(2)}, 1000); 
        setTimeout(function(){console.log(3)}, 0); 
        console.log(4);
    })();

Best Solution

Here's a great explanation by John Resig: http://ejohn.org/blog/how-javascript-timers-work/
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.