JS Runtime / Execution context
Tags: english, runtime, javascript
Published: 2024-06-30 02:27:28

JavaScript runtime is the environment or engine required to run JavaScript code. These runtime environments parse and execute JavaScript code. JavaScript is the only language that runs in the browser.

Runtime types and operating environment:
1-Google chrome (Browser)

Browsers are the main runtime environment of JavaScript. Each browser has its own JavaScript engine:

- Google Chrome: V8 engine.
- Mozilla Firefox: SpiderMonkey engine.
- Safari: JavaScriptCore (Nitro) engine.
- Microsoft Edge: Chakra old versions and V8 new versions.

In the browser JavaScript runtime environment, JavaScript code can be used together with HTML and CSS.

2-Node.js

Node.js is a runtime environment for running JavaScript on the server side. This environment is based on the V8 engine and allows JavaScript code to be executed outside the browser.
_Nodejs technology runs javascript codes outside the browser._
- Server scripts: HTTP servers, APIs, etc.
- Asynchronous performance: The asynchronous nature of Node.js makes it highly efficient and fast.
- Large ecosystem: There are many modules and libraries for Node.js.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19w49f09e0r11gi5ic6q.png)

**Execution context**
In the JavaScript programming language, the execution context is the environment that contains all the information necessary to execute the code. Each executable code has its own execution context. Execution context contains the following elements:

- Variable Object 
All variables, functions and arguments are stored here.
- Scope Chain
Scope chain represents the search order of variables and functions. Each execution context has its own scope chain, and this scope chain contains references to the outer parent execution contexts.

- this Keyword
The this keyword can refer to different objects depending on the execution context.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18mlpa8ipdwda924mosb.png)

**Types of Execution Context**

There are 2 main types of execution context in JavaScript:

1-Global Execution Context:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mhaz42tc95xmjpfmpgi.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ycwljoim7y4e531e5c8.png)


2-Function Excution Context:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcghlkzrznkke6pdh7ag.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zu1plyy26y29mzbt6sm8.png)

**javascript in  global execution vs function execution**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xy08j6aa2qmzl1q4abe3.png)

Execution context:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukmip4nz965y8dw5k8z3.png)

**Execution Context**

When JavaScript is running in a browser, it cannot be understood directly, so it must be converted into a machine-readable language. When the browser's JavaScript engine encounters JavaScript code, it "translates" the JavaScript code we wrote and creates a special environment that controls the execution. This environment is called Execution context.

***Execution context** can have **global scope** and **function scope**. When JavaScript first runs, it creates a **global scope**.*

*Next, the JavaScript **parses** and stores the variable and function declarations **in memory**.*

*Finally, the code will initialize the variables stored in memory.*

Execution context is a block of data opened by JavaScript for each block of code, which contains all the information needed for the currently running code. For example, the variables/functions/this keyword


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rntetnscmgiu6ljtuqsi.png)

- Creation phase - variables declared with var are assigned **undefined**, variables declared with let are **uninitilized** and functions are read.
- Execution phase - variables are assigned values ​​and functions are called.