First console.log('a') executes and logs 'a' to the console. Then all the setTimeouts are read and moves it away from the main event loop to the call stack. Then console.log('e') executes and logs 'e' to the console.

Then we would expect console.log('d') to execute as it has 0ms of timeout but console.log('b') will get executed first having 1ms of timeout. This happens because the timeout of 1ms has elapsed till the time all the statements are read and console.log('e') was executed.

Now we have both console.log('b') and console.log('d') (also ready as timeout of 0 ms also gets elapsed) ready to be executed. As console.log('b') came first into the call stack therefore it will be executed first, logging 'b' and then console.log('d'), logging 'd'.

And finally when 10ms of timeout is elapsed console.log('c') gets executed and logs 'c'.

For useful and amazing frontend and programming tutorials: