How JavaScript works BTS?

Sanidhya Pawar
2 min readMar 30, 2021

Javascript is the preferred language by almost all the web developers out there. Even I was surprised initially how versatile this interpreted beast is. At first this may sound like a normal language which has some basic syntax and you are good to go, but let me warn you at this moment itself, you are in a wrong assumption. This is so light weight and powerful language that has umbrella of concepts beneath it. The best example where we often interact with JS is browser itself. Browser is not just about searching around the web pages efficiently, but it provides a environment where JS can be executed efficiently. One of the many advantages of JS is that the language is Single Threaded and Synchronous. The first question that should pop out is then how come the language is known for its asynchronous nature. The simple answer to that is the environment in which it is running in, the background environment has bunch of superheroes that make this language asynchronous. The unsung heroes are Event Loop, Callback queue, Microtask queue. Whenever something has to be executed that module is sent to the Call Stack to be executed. The state of art, event loop handles every thing behind the scene and simply provides the modules to be executed to the Call Stack; hence Call Stack is not present in the super hero’s list. Lets take one example-

Lets say I am calling some API to get some data from it, I will make a request to the API from my Frontend Microservice, so when this request will be made until the request is completed and it returns the data as expected the subsequent code in your Frontend Microservice will be executed, and once the request is completed the response will be stored in a Microtask queue, and event loop will infinitely monitor for completion of the tasks in the Microtask queue. As soon as it finds that the Microtask is not empty and it has something, it deques the completed module and pushes it onto the call stack to be executed. And the the callback function or the next set of code is executed that was supposed to handle the returned data.

--

--