# Javascript Runtime Env

<mark style="color:purple;">JS</mark> <mark style="color:purple;"></mark>*<mark style="color:purple;">runtime environment</mark>* <mark style="color:purple;"></mark><mark style="color:purple;">is where our javascript code will be executed.</mark>

JS Runtime (as implemented in a browser) comprises of below modules or parts or components.

* Web APIs
* JS Engine(like Chromium's v8 engine)
* Call or Execution Context stack
* Event loop / Callback Queue
* Microtask Queue

Javascript => Single Threaded language\
&#x20;                   \=> which means it has a single call stack\
&#x20;                     \=> it can do one thing at a time

### JS Engine

\= program which compiles, parses and executes a Javascript code

as it starts executing a JS code, it will create:

&#x20;\=> a <mark style="color:orange;">Memory Heap</mark> and a <mark style="color:orange;">Call stack</mark>

<mark style="color:orange;">Memory Heap</mark> = an unstructured memory pool

&#x20;                        \= where it will store variables, objects from the code

<mark style="color:orange;">Call Stack</mark> = stack of code instructions that JS Engine finds in the code when it starts executing it from top to bottom

&#x20;                \= where JS code gets executed using execution contexts

&#x20;                \= a stack of Execution contexts = a Global Env. Context + other execution contexts as they are created and removed for each function or curly bracket pairs

| Web API                                                                                                                                                                                                                                                                                                                       |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p>= an API of functions to allow javascript code to interact with browser or use browser's functionality (for e.g.  console, setTimeout, the DOM object and its various functions, fetch etc.)</p><p></p><p>= provided by browser</p><p></p><p>= therefore, Web API objects and functions can be used in javascript code</p> |

| Callback Queue                                                                            |
| ----------------------------------------------------------------------------------------- |
| = a data structure that contains all the callback functions that are ready to be executed |

| MicroTask Queue                                                                                                          |
| ------------------------------------------------------------------------------------------------------------------------ |
| It stores special callback functions which have higher priority than the callback function waiting in the callback queue |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bharat-tiwari.gitbook.io/our-tech-journal/javascript/javascript-runtime-env.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
