DEV Community

Ilya R
Ilya R

Posted on

JS LexicalEnvironment - short overview

Lexical environment is a part of context in JS. When code start running every functions, methods and block of code create their own LexicalEnvironment.

LexicalEnvironment - is a special object in JS. This object is created and called every time when code executing. If the same function is running multiple time, then every time Lexical Env will re-create inside that function.

In code we do’t have access to object of **LexicalEnv **and we can’t change that object directly. JS “engines” do it for us. They optimise Lexical Environment’s object, clean up memory by deleting variables that no longer used, etc.

The Lexical Environment object consist of:

  • Environment Record - object which store all using variables in block of code.

  • [[Environment]] - link on outer Lexical Environment. This thing using for access to Lexical Environment of “a parent’.

And in fact, JS variable is a property of an Environment Record of LexicalEnvironment. To change a variable means to change a property of LexicalEnvironment.

Discussion (0)