DEV Community

BekmuhammadDev
BekmuhammadDev

Posted on

1 1 1 1 1

JS Runtime / Execution context

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

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

Types of Execution Context

There are 2 main types of execution context in JavaScript:

1-Global Execution Context:

Image description

Image description

2-Function Excution Context:

Image description

Image description

javascript in global execution vs function execution

Image description

Execution context:

Image description

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

  • 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.

Neon image

Resources for building AI applications with Neon Postgres 🤖

Core concepts, starter applications, framework integrations, and deployment guides. Use these resources to build applications like RAG chatbots, semantic search engines, or custom AI tools.

Explore AI Tools →

Top comments (1)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay