Here we'll talk know about the scope and lexical environemt.
Let's dive into the codes directly, understand the below codes first:
What will be the output for "consle.log(b);" ?
It'll print 10. WOOWWWW, but why ?
Let's have a look in a different way.
What will be output of this?
The output is 10, WoooWWW..
Again, "b" is accessible in that inner function. why ?
Again the output is 10, Let's change something else.
Output:Uncaught ReferenceError: b is not defined.
What happend in the above code?
Scope: Scoep defines where you can access the variables or function in our codes
So the question comes here from the above code:
What is the scope of variable "b" in the above code? It means where can I access the variable "b" in our codes? Or is "b" insode the scope to access?
So we need to find out the answer of the above question, before this let's know about the Lexical Environment.
Lexical Environment?Let's have a look again into below codes:
Let's see how this will be executed in call stack in the bewlo image:
So, whenever the Global Execution Context is created, lexical environemt is also created. Lexical Environment is the local memory along with the lexical environemt of its parent.
Lexical is a term means in hierarchy or in sequence.So in the above codes, the function "c" is lexically sitting inside the function "test", means the function "c" inside the function test and the function test is lexically sitting inside the global scope.
Conclusion:Lexical Environemt is creted whenever the Execution Context is created.
Lexical Environment is the local memory with lexical environemt of its parent (lexical parent - where actually parent physically present in the codes).
The chain of the lexical environemt references are known as scope chain. the scope chain defines wether a variable or function is present inside the scope or not. If the scope chain is exhausted and variable is not present that means that means it is not inside the scope chain and variable will be undefined.
Support our IDKBlogs team
Creating quality content takes time and resources, and we are committed to providing value to our
If you find my articles helpful or informative, please consider supporting us financially.
Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce high-quality content.
Thank you for your support!
I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go.
If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.