관리 메뉴

Silver Library (Archived)

What does 'Lexical environment' mean in JS? 본문

Fundamental of CS/Knowledge

What does 'Lexical environment' mean in JS?

Chesed Kim 2023. 1. 1. 16:59
반응형

So, what does it mean? Here is for the record.

 

In JavaScript, a lexical environment is:

A data structure that holds information about the variable bindings, that are active at a given point in the code.

 

It consists of an outer lexical environment and an inner lexical environment.

1. The outer environment is the one that is associated with the code that surrounds the current block of code,

2. The inner environment is the one that is associated with the current block of code.

 

Furthermore, variable bindings in JavaScript are created when a variable is declared using the var keyword.

When a variable is declared, it is added to the current lexical environment, along with its value. If the same variable is declared in an inner lexical environment, it is given precedence over the binding in the outer environment.

 

So, this lexical environments are used to keep track of the variables that are in scope at a given point in the code, and to resolve references to those variables when they are used.