Lexical environment and function scope

Is lexical environment and scope in javascript one and the same thing?


Solution 1:

Giving the answer based on what I have just learned from 'Secrets of the Javascript Ninja, 2/e' --

They are different concepts but related, we need to define a related concept - Execution Context & it's stack to understand.

Execution Context & Execution Context stack : Execution context is the internal javascript construct to track execution of a function or the global code. The js engine maintains a stack data structure - execution context stack or call stack, which contains these contexts and the global execution context stays at the bottom of this stack. And a new execution context is created and pushed to the stack when execution of a function begins. A particular execution context tracks the pointer where statement of the corresponding function is being executed. An execution context is popped from the stack when corresponding function's execution is finished.

Lexical Environment : it's the internal js engine construct that holds identifier-variable mapping. (here identifier refers to the name of variables/functions, and variable is the reference to actual object [including function type object] or primitive value). A lexical environment also holds a reference to a parent lexical environment.

Now, for every execution context -- 1) a corresponding lexical environment is created and 2) if any function is created in that execution context, reference to that lexical environment is stored at the internal property ( [[Environment]] ) of that function. So, every function tracks the lexical environment related to the execution context it was created in.

And every lexical environment tracks its parent lexical environment (that of parent execution context). As a result, every function has a chain of lexical environments attached to it. [Note: in js a function is an object, creating a function by a statement means creating an object of type Function. So like other objects, a function can hold properties both internal and user defined]

Scope : it's the language agnostic concept, to refer to the visibility of variables or functions to the executing code. In js a variable or function is visible to the executing code, if it is there in the current lexical environment or in the lexical-environment-chain of the enclosing function. In case of global code, the chain does not exist.

Hope, you understand now ..

Note: similar to function's case, by the introduction of let and const in es6, when a block begins to execute (if block, for loop block etc), a new lexical environment is also created having the parent function's lexical environment as parent.

Solution 2:

Here's what the spec says about lexical environments:

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 ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment.

Based on that, I would say yes, that's what people are usually talking about when they say "scope".

Although it could probably be argued that a "scope" is actually defined as a "Declarative Environment Record":

Each declarative environment record is associated with an ECMAScript program scope containing variable and/or function declarations. A declarative environment record binds the set of identifiers defined by the declarations contained within its scope.

If you think of a "scope" as a thing that contains bindings between identifiers and values, then the 2nd definition probably fits better. If you think of it as something that is aware of its ancestor scopes, then the first definition fits better.

Edit: and a third option is "Execution Context".

Solution 3:

Lexical Environment is the environment of the function where it is written. That is, the static order/place where it is situated, regardless from where it is called from.

Scope of a variable/function is basically the locations from where a variable is visible/accessible.

Execution context is the status of the execution stack at any point during runtime. That is the current execution context.