In what order will the numbers 1-4 be printed to the console when the code below is executed?
(function() {
    console.log(1); 
    setTimeout(function(){console.log(2)}, 1000); 
    setTimeout(function(){console.log(3)}, 0); 
    console.log(4);
})();
Explanation
The values will be logged in the following order:
1
4
3
2
1 and 4 are displayed first since they are logged by simple calls to console.log() without any delay;
2 is displayed after 3 because 2 is being logged after a delay of 1000 msecs (i.e., 1 second) whereas 3 is being logged after a delay of 0 msecs.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback