Surprisingly, none of the answers given here mention the relevance of the execution context in the scope chain.
The JavaScript engine places the currently executing code in the execution context. The underlying execution context is the global execution context. Each time a new function is called, a new execution context is created and pushed onto the execution stack. Think of a stack stack located on a call stack in other programming languages. Last in the first came out. Now each execution context has its own environment variable and external environment in JavaScript.
I will use the example below as a demonstration.
1) First, we enter the phase of creating a global execution context. Both the external environment and the variable environment of the lexical environment are created. The global object is configured and placed into memory with a special variable "this" pointing to it. The function a and its code, as well as the variable myVar with an undefined value, are stored in memory in a global environment variable. It is important to note that the code function is not executed. It just fits in memory with function a.
2) Secondly, this is the execution phase of the execution context. myVar is no longer an undefined value. It is initialized with a value of 1, which is stored in a global environment variable. The function a is called and a new execution context is created.
3) In the "Execution Context" function, it goes through the phase of creating and executing its own execution context. He has his own external environment and a variable environment, that is, his own lexical environment. The function b and the variable myVar are stored in their environment variable. This environment variable is different from the global environment variable. Since the function a is lexically (physically in the code) at the same level as the global execution context, its external environment is the global execution context. Thus, if function a should refer to a variable that is not in its variable environment, it will search the chain of scopes and try to find the variable in the variable environment of the global execution context.
4) Function b is called in function a. A new execution context has been created. Since he is in function a lexically, his external environment is this. Therefore, when it refers to myVar, since myVar is not in function b of the Variable Environment, it will look in function Variable Environment. He finds it there, and console.log prints 2. But if the variable was not in the Variable Environment function, then since the Outer Environment function is a global execution context, the scope chain will continue to search there.
5) After completion of the execution of functions b and a, they are removed from the execution stack. The single-threaded JavaScript engine continues execution in the global execution context. It calls function b. But in the global environment of variables there is no function b, and there is no other external environment for searching in the global context of execution. Thus, the JavaScript engine throws an exception.
function a(){ function b(){ console.log(myVar); } var myVar = 2; b(); } var myVar = 1; a(); b(); > 2 > Uncaught ReferenceError: b is not defined
The following example shows a chain of scopes in action. In function b, the Runtime Context Variable Environment is missing myVar. Thus, he seeks his external environment, which is a function of a. Function a also does not have myVar in its variable. Thus, the Search Engine performs the function External Environment, which is the global External Environment of the execution context, and it defines myVar. Therefore, console.log prints 1.
function a(){ function b(){ console.log(myVar); } b(); } var myVar = 1; a(); > 1
Regarding the execution context and the associated lexical environment, including the external environment and the environment variable, it is possible to define variable areas in JavaScript. Even if you call the same function several times, for each call it will create its own execution context. Thus, each execution context will have its own copy of the variables in its environment of variables. There is no separation of variables.