The last hope that we will talk about is called lexical scope.
So what is laser scope?
So let me comment this once.
Now bring the function scope down here and change this one to less lexical scope.
Okay, so save it.
And there we go.
Okay, a little bit.
A little bit.
Yeah.
Good.
So what is lexical scope in JavaScript?
It refers to the visibility of variables and functions in relation to the lexical structure of the code.
That is the scope of a variable.
A function is determined by H position in the code.
For example, variables in function declared inside a function are only visible inside that function.
That one we know.
And again, lexical scope is an important concept in JavaScript because it affects how code is executed
and how variables are accessed.
But this is also a complex topic again, so and there are a few different ways to think about it.
But let me break it down.
One way to think about lexical scope is in terms of the nesting of functions.
I mean, we are going to nest a function together and see how we can access variables across these nested
functions.
So the scope of the inner function is said to be lexical and close by the scope of the outer function.
And this means that the inner function has access to variables and functions declared in the outer function,
but not vice versa.
So let me prove you something here about lexical scope.
For this one, we are going to next function together and see if we can access variables both the nested
and the outer function.
Great.
So let's declare a function.
Let's say my function one.
All right.
And dysfunction will not take anything great.
And inside this function, let's also declare other functions inside.
So function is going to be another function.
Remember, because functions are first class citizens, we can pass a function, a function, and we
can return a function from a function.
After this one, we are going to dive more about how we can create a function.
So let's say we have a function called another function.
Being inside my function and I has that and this a perfect function call.
So let's say that inside my outer function of my inner function, I'm going to declare a variable C.
S, which represents.
Hello.
As that.
And inside the honor function, I'm also going to declare a variable, say y.
So let's say here, let's y also equal to let's say ten.
Perfect.
So let's try to console.log the x in my first function.
Now let's call this function down here.
It's going to be my function one if I save it.
Now, you see, I have.
Hello.
So how can you also call this function?
This function here?
Dysfunction.
Because it is his cope with this one.
We can also call the function inside my first function.
So call, function or invoke for short in work.
So this one too let's console.log the y and let's see.
So now see that this function has been called, but this one hasn't been called because we haven't invoked
the function.
So come down here, let's say another function, call it.
Let's save it.
Now I have the two functions being called, but here we go.
Even though we have a function scope here.
But inside this function we have two things going on.
We have a variable here and we have another function here.
So a variable that we declared outside this function which is X can be accessed inside my inner function.
Have a look.
If I try to console.log x, let's console.log x, k and let's see.
Now everything works fine.
Line number 54, which is this line of T four.
So it works.
But if I try to access this Y outside this function inside my outer function here, so let me change
this one to Y now let's see.
Now I get an error.
So take note about this again.
A variable that we declared outside the inner function can be declared or can be console.log or can
be found or can be used inside the inner function.
But a variable declared inside my inner function cannot be accessed outside the inner function.
That's about the physical scope.