Execution Context & Lexical Environment
Execution context is a concept in the language spec that — in layman’s terms — roughly equates to the ‘environment’ a function executes in; that is, variable scope (and the scope chain, variables in closures from outer scopes), function arguments, and the value of the this object. — Stack Overflow
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. — Stack Overflow
How I understand it. The Execution Context is the environment a function executes in. Lexical Environment is the environment your code is written in. The way to think about it is that each function or code you write is its own planet or world and each world has its own environment / lexical environment and those worlds also create their own execution context.
Variables with keyword var are only partially hoisted. Meaning the variable gets hoisted but not the value of the variable, thus getting undefined. Also, let and const don’t get hoisted. Side note, hoisting happens on every execution context.
Within each execution context is a special object called a scope chain which is used to resolve variables. A scope chain is essentially a stack of currently accessible scopes, from the most immediate context to the global context. — Toptal
How I understand it. When I think of scope chain I relate it to a chain of command. Let’s say there is a business of three people. There is a CEO, a Manager, and one Employee. In code, this would be a function with a variable that returns another function with a variable that returns one more function with a variable.
If you are the CEO, you could request a meeting with anyone at the company. But if you're the employee, you can only request a meeting with the manager, you can't go straight to the CEO. Scope chain is the same. The main function can access the nested function's variables but the nested functions can only access the variables of the function above it
This week was another awesome week of learning. This blog was no way intended to be an expert point of view. It’s just how I understood these concepts. I welcome all readers to leave any comments that you might think will better help me understand these concepts, or if there are any concepts that you think I’m not really understanding. Thanks for reading see you next week.