They are different, the first adds a function to the event queue so that it can be executed as soon as it gets a chance after completing the current execution path. The second will execute it immediately.
For instance:
console.log('first'); setTimeout(function(){ console.log('third'); }, 0); console.log('second');
The order in which they are printed is clearly defined, you can even do something slow (but synchronous) before printing “second”. This ensured that console.log('second'); will be executed before setTimeout callback:
console.log('first'); setTimeout(function () { console.log('third'); // Prints after 8 seconds }, 0); // Spinlock for 3 seconds (function(start){ while(new Date - start < 3000); })(new Date); console.log('second'); // Prints after 3 seconds, but still before 'third' // Spinlock for 5 seconds (function(start){ while(new Date - start < 5000); })(new Date);
source share