Sitemap
CodeScoop.dev

An initiative dedicated to empowering communities by driving real-world problem solving. We enable individuals to build practical solutions while strengthening their ideation, development, collaboration, and leadership skills.

What has changed in Lexical Environment as per ECMAScript2021

Explore the lexical Environment & Environment Record in Javascript 2021

2 min readAug 17, 2022

--

Press enter or click to view image in full size
Photo by Paul Skorupskas on Unsplash

Let’s first understand the Lexical Environment & Environment Recordas per different versions of ECMAScript Specification.

Get Yogesh Yadav’s stories in your inbox

Join Medium for free to get updates from this writer.

From ES2015 till ES2020 Specification:-

Lexical Environment:

  • A lexical environment is a specification type used to define the association of Identifiers to specific variables and functions, based upon the lexical nesting structure of your code.
  • A lexical environment consists of two components:

    1. Environment Record:
    It records the identifier bindings that are created within the scope of its associated Lexical Environment. It is referred to as the Lexical Environment’s EnvironmentRecord.

    2. Outer Reference:
    A reference to the outer environment (null in the global environment).

A conceptual view using pseudo-code:

Note: The [[Evironment]] created inside Execution Context is of type Lexical Environment [refer ES2020]

Press enter or click to view image in full size
Lexical Environment in Execution Context

According to the 12th Edition ECMAScript2021 Specification:

Environment Record

  • An Environment Record is a specification type used to define the association of Identifiers to specific variables and functions, based upon the lexical nesting structure of your code.
  • Every Environment Record has one component:

    1. Outer Reference
    An [[OuterEnv]] field, which is either null or a reference to an outer Environment Record.

A conceptual view using pseudo-code:

Note: The [[Environment]] created inside Execution Context is of type Environment Record [refer ES2021]

Press enter or click to view image in full size
Environment Variable in Execution Context

Let’s also understand the `Structure of execution context`

Execution Context:

  • An execution context is a specification device that is used to track the runtime evaluation of the code.
  • To keeps the track of execution progress of its associated code, it needs various state components like LexicalEnvironment, VariableEnvironment, etc.

In pseudo-code:

Note:

Press enter or click to view image in full size
Lexical Environment vs Environment Record in JavaScript 2021

Summary

Let’s have a quick recap of all the steps we perform in the above code snippet.

1. In ECMAScript2021, the [[environment]] which is created inside the execution context is of type Environment Record instead of Lexical Environment.
2. So, The LexicalEnvironment component and VariableEnvironment component of an execution context are always Environment Records.

Wrap Up!!

Thank you for your time!! Let’s connect to learn and grow together.
Github Twitter

--

--