Understanding Event Loop, Call Stack, Event & Job Queue in Javascript
Understanding Event Loop, Call Stack, Event & Job Queue in Javascript In this article we will dig into how javascript works under the hood, how it executes our asynchronous javascript code, and in what order (Promise vs setTimeout), how it generates stack trace and much more.. As most developers know, that Javascript is single threaded, means, two statement in javascript can not be executed in parallel. Execution happens line by line, which means each javascript statements are synchronous and blocking. But there is a way to run your code asynchronously, if you use setTimeout() function, a Web API given by browser, which makes sure that your code executes after specified time (in millisecond). Example code: console.log('Message 1'); // Print message after 100 millisecond setTimeout(function() { console.log('Message 2'); }, 100); console.log('Message 3'); setTimeout takes a callback function as first parameter, and time in millisecond as second paramet...