Lexical environment and function scope

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 that tracks 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. The global execution context stays at the bottom of this stack. A new execution context is then 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 the corresponding function’s execution is finished.

Lexical Environment is 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 the 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.

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 with a statement means creating an object of type Function. So, like other objects, a function can hold properties both internal and user defined.

Scope is a language agnostic concept that refers 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 the function, via 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 with the parent function’s lexical environment as parent.

Leave a Comment