What is a Runtime Environment?
A runtime environment is where your program will be executed. It determines what global objects your program can access and it can also impact how it runs.
How does the JavaScript Environment Work?
The JavaScript runtime environment provides access to built-in libraries and objects that are available to program so that it can interact with the outside world and make the code work.
Different browsers have different JavaScript Runtime Environments.
In the context of a browser, JS runtime is comprised of the following elements:
- The JavaScript Engine
- Web APIs
- The Call Back Queue
- The Event Loop
The JavaScript Engine
All the browsers have a JavaScript Engine. But all are not necessarily the same engine. Also, all the engines follow the same standard although their implementation is different.
For example, Google Chrome has the V8 engine and Mozilla Firefox has Spyder Monkey as a JavaScript engine.
The JavaScript Engine parses the code for us. It has two major components:
- Memory Heap
- Call Stack
Memory Heap
A memory heap is a section of unstructured memory that is used for the allocation of objects and variables.
Call Stack
The call stack keeps the track of where we are in the program and runs in a last-in, first-out way.
Web APIs
There are a large number of APIs available in web browsers that allow us to do a wide variety of operations like DOM manipulation, Fetch APIs, Drawing graphics, etc., all these types of operations are carried out on Web APIs.
Callback Queue
The callback queue stores the callback functions sent from the Web APIs in the order in which they are added.
Event Loop
The job of the event loop is to constantly monitor the state of the call stack and the call back queue. If the call stack is empty, it will grab a callback from the callback queue and put it onto the call stack, scheduling it for execution.
Comments
Post a Comment