📌 What is scope?
The space in which variables and statements are available is known as scope.
It allows variables with the same name to exist without colliding, and it prohibits outer scopes from having access to inner scopes.
The space in which variables and statements are available is known as scope.
It allows variables with the same name to exist without colliding, and it prohibits outer scopes from having access to inner scopes.
📌 Types of scope :
In JavaScript we have three types of scope:
1. Global scope
2. Function/local scope
3. Block scope
In JavaScript we have three types of scope:
1. Global scope
2. Function/local scope
3. Block scope
📌Local scope or Function scope :
It is created inside the function and declarations inside the function are only accessible there.
It is created inside the function and declarations inside the function are only accessible there.
When the `second()` function is executed it finds that `myName` and `age` are not in local scope
So, how is JavaScript able to reference it?
So, how is JavaScript able to reference it?
When the constant is not found in the local scope JavaScript reaches the parent scope getting Jonas and 30 by variable lookup
variable lookup:
When a variable is not in the local scope, the engine looks up in the parent scope chain until it finds the variable it’s looking for.
variable lookup:
When a variable is not in the local scope, the engine looks up in the parent scope chain until it finds the variable it’s looking for.
As the value of job (teacher) is present in local scope there is no need of variable lookup.
It is important to know that the scope chain works only one way, from the inner scope to the outer scopes.
The scope chain doesn't affect by order of function.
It is important to know that the scope chain works only one way, from the inner scope to the outer scopes.
The scope chain doesn't affect by order of function.
I have written blog on Conditional Statements in JavaScript on hashnode.
Check it out here :
dhrumishah.hashnode.dev
Check it out here :
dhrumishah.hashnode.dev
Thanks for reading this thread ❤
If you like it , make sure you:
🔷Like the tweet
🔷Retweet the first tweet⚡
For more content , Follow:
@deetwts
If you like it , make sure you:
🔷Like the tweet
🔷Retweet the first tweet⚡
For more content , Follow:
@deetwts
Loading suggestions...