It's time to talk about another very important aspect in JavaScript, especially when we think about
how JavaScript works behind the scenes.
So in this lecture, you will learn about coping and sculpting sometimes, especially when you are new
in JavaScript.
It's a bit confusing and not understandable concept.
So let's make it clear.
What is the scope?
We can consider scopes regarding variables and functions.
Scope in programming language refers to the visibility or accessibility of variables and functions.
In other words, it tells you where in your program you're allowed to use the variables and functions
that you have defined.
OK, suppose that we have some JavaScript code.
Here is the variable number one, then we have function AI inside it.
We have another variable number two, then below we have another function.
Function B and it contains another variable.
Number three, if we consider this code from the variable perspective, we can say that the location
where you declare a variable will affect where it can be used within your code.
So here, variable number one is created on global level, which literally refers to global window object.
In other words, we call variables global when they are defined outside the function.
So the global variable has a global scope, it means that from everywhere in your code, we can access
on that.
In our case, no one is a global variable and it has a global scope.
From the fashion perspective, we can say that in JavaScript, each function creates a new scope.
So here Function A creates its own local scope and the variable number two, which is defined inside
function A is a local variable because it's not accessible outside of the function A.
So local variables are variables that are defined within functions.
They have local scope, which means that they can only be used within the functions that define the.
Inside the function, we have Function B, which in the same way creates its own local scope, and in
this local scope we have another local variable, number three.
Which is not accessible from outside function B.
All right, let's consider once more the accessibility on the variables that we have defined for function
B, there are outer environments which are a local scope of function A and the global scope itself.
Those outer environments are also called as apparent scope in the same way we can call local scope or
function A as a child scope for global scope and the local scope of function B as that child's scope
for function ET.
As for function eight, its current scope is out global scope where it's lexically defined.
A Function B wants to use the variables from its parent scope's, this case, variable number two and
number one.
It's acceptable in JavaScript.
In the same way function, it can use the variable from its outer environment, which is in this case,
number one.
But what happens if Function A wants to use the variable from function B. I mean, the variable number
three, it's not possible because as we said, local variables are not accessible from the outer scopes
and in the same way from the global scope.
We cannot access on the variables, number two.
And number three, because they are local variables of these relationships.
Among those scopes is called scope chain.
So, again, accessibility on the variables is possible from the inner to outer scopes, and it's not
possible from the outer to inner scopes.
All right, let's discuss once again what this Coke chain is using some practical examples, open the
text editor, use the same example that we have talked about and write verbal No.
One equals to five.
That create Function A.
And inspired you to write very well, number two.
Equals the 10.
And below, right function B.
And inside, it creates another variable.
Number three equals 250.
So you have variable number one, which is created on global scope, their function day in which we
have local variable number two and function B, which is inside function eight.
And it also contains a local variable, number three.
Suppose that we want to add those numbers so right inside function B, cancel that log.
Number three plus.
Number two and number one.
In order to get the results, we need to invoke Function B.
And to execute it, we need to call function A as well.
So reload the page and we have 30, which is the sum of those three numbers.
This example proves that we can access on the variables from the inner scope's.
From function B, we have access on a variable number to add verbal number one, but what happens if
we add those numbers inside the function a bit outside function B, I'm sure that you already know the
answer.
So reload and we have an error, which tells us that number three is not defined.
So function A could access our number one, which is in its periscope, but it couldn't have access
on number three, which is inside a local scope of function B, and this code for function is child's
called.
If we get rid of that number three, then the code will work.
OK, let's move up this line of code and get the result on global level.
You see that still we have an error, which tells that number two is not defined.
The reason is the same from the global scale.
We cannot access on number two as well.
All right, let's change the names of variable number two and number three and write number one for
both of them.
Then look, I left them in council.
It seems that a of those variables are the same, but in different scopes, they don't change their
values, they're just different variables.
In order to prove that, once again, log no one in council after calling function a right.
Cancel that log.
No one.
Reload and I see that on global level.
Number one is five in both cases, but in local scopes they are different variables and have different
values.
All right, let's demonstrate another case and common discourse looks out.
And in function B, let's leave, just cancel like no one.
So what do you expect to get in function B. We have no longer verbal No.
One.
So you might think that we will get undefined.
Let's see, reload and we have 10.
Why, because of this corruption, if we come at this line, then we will get five.
OK, let me explain once again, when function B couldn't find no one in its local scope, then it checked
its apparent scope, which is a local scope or function A and there it found this variable.
So we got the value tat in council.
But if in the function a verbal number has exist.
Then Function B would check the global scope where it could find variable number one, which equals
to five.
Therefore, we've got value five in concept.
All right, so in this lecture, we have learned what a scope and scope, Jane.
You need to know all this stuff well, because it's very important and it will help you in writing the
current code.
That's more.