Part 8: Scope Chain, Scope and Lexical Environment π§
In this article, we will cover:
- What is Scope?
- What is Lexical Environment?
- What is Scope Chain?
******************************************************************
Scope in JS is directly related to lexical Environment. Letβs take below example:
function a(){
console.log(b);
}
var b = 10;
a();
When JS engine reach line 2 and try to execute. JS engine will look for βbβ in local memory space of function a. But it wonβt be there because we never created βbβ inside function a().
What will happen now? Will it print undefined or not defined or print value of variable?
Letβs run and see output
So, it printed value somehow.
Letβs make it more complicated. Add another function inside function and try to print βbβ inside that. Invoke function also. What will happen now?
function a(){
c();
function c(){
console.log(b);
}
}
var b = 10;
a();
Letβs run and see output again
So, it again printed value again.
Letβs take another example and move variable βbβ inside the function but try to access it in global scope by printing it. What will happen now?
function a(){
var b = 10;
c();
function c(){
}
}
a();
console.log(b);
Letβs run and see output again
Here goes ERROR. which says not defined.
Now, Scope comes into picture.
Scope means where you can access a specific function or variable .
It mainly have two aspects:
- What is scope of this variable βbβ ? Where can i access this variable βbβ ?
- Is βbβ inside the scope?? Like Is βbβ inside the scope of function c?? Which means Can I access βbβ inside function c??
What happen when program runs?
function a(){
var b = 10;
c();
function c(){
}
}
a();
console.log(b);
- When we run this program, GEC is created and pushed inside Call Stack.
- When you run program, it will try to assign values to global variables and function. Example : It will assign value to function a().
- Then it will invoke the function a(), which will result in creating execution context for a().
- Now, function a() will reserve memory for variable b and function c().
- So variable b= undefined and function c = function reference
- Once, code starts executing b = 10.
- At line 3, function c is invoking which results in new execution context and pushed to call stack.
What is Lexical Env?
Where ever an execution context is created, a lexical env is also created. So, Lexical env is local memory, along with lexical environment of itβs parent.
Lexical means hierarchy or in sequence.
In below example, function c() is lexically sitting inside function a(). And function a() is lexically inside the global scope.
function a(){
var b = 10;
c();
function c(){
}
}
a();
console.log(b);
So, Letβs assume orange box inside memory which represents lexical env of itβs parent.
1. In case of function c() execution context, it will represent lexical env of a() and a()βs parent i.e GEC.
2. In case of function a() execution context , it will represent lexical env of GEC.
3. In case of GEC, it represents outer env which is null.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
What happens if you try to do console.log for b inside function c( ) ?? Will it print value of b or gives not defined?
function a(){
var b = 10;
c();
function c(){
console.log(b);
}
}
a();
console.log(b);
- When JS engine encounter this line, it tries to find out this inside the local memory of function c().
- It wonβt find it because there is no variable b inside function c() memory.
- Now, JS engine goes to orange reference which goes to lexical environment of itβs parent which is function a()βs memory.
- Now, it find variable b inside function a()βs memory.
- It goes back and prints value of b as 10.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β -
If variable b is moved outside function a(). What happens if you try to do console.log for b inside function c( ) ?? Will it print value ?
function a(){
c();
function c(){
console.log(b);
}
}
var b = 10;
a();
console.log(b);
Answer is , YES. Check below for explanation β¬οΈ
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
If variable b is removed. What happens if you try to do console.log for b inside function c( ) ?? Will it print value ?
function a(){
c();
function c(){
console.log(b);
}
}
a();
Answer is , NO. Check below for explanation β¬οΈ
Output:
Here, we can say: βb is not in the scopeβ.
So, this whole mechanism of searching b in local memory and if not found then into reference of the outer parent lexical env and so on is known as βSCOPE CHAINβ
Summary:
1. Lexical environment is created whenever an execution context is created. So, Lexical environment = Local memory + Reference to lexical env of parent.
2. Parent i.e Lexical Parent is where actually that function sits inside the code.
3. Chain of Lexical Env is known as Scope Chain. And it defines whether a variable or function is present inside the scope or not.
Letβs see same in browser as well:
- Call stack with GEC, a()βs execution context and c()βs execution contextβ¬οΈ
2. function a() have Local memory + lexical env of parent as βGlobalβ β¬οΈ
3. function c() have Local memory + lexical env of parent i.e a()βs memory + lexical env of itβs parentβs parent i.e a()βs parent which is GEC β¬οΈ
Note: You can see βClosureβ keyword in above screenshot. Itβll be covered in future articles.
βοΈHappy Coding. βοΈ
Hopefully, you learned something today! Before you go:
- π Clap for the story and follow Swati Redhu π©π»βπ»
- π° View more content in the JavaScript learning roadmap 2023 β The Ultimate Guide β€οΈ
- π Follow on : LinkedIn