In this video we're going to discuss a concept called scope in javascript.
Scope refers to the context that some code is being executed in.
So wherever we have some code let's say it's inside of a function called Say hello.
That code is running in a specific context.
And so it might have some variables and might have some properties and scope is referring to whichever
variables and properties are visible in that function.
And that could be different inside of another function or if we had functions nested inside of each
other.
Which is something you can do.
We might have different scopes and different variables are visible.
So this will make sense with a few examples.
This is definitely a notoriously difficult and complicated topic but I'm going to try my best to simplify
it for you.
Let's start with something simple here.
Declare function do math and all that do math we'll do is declare variable x equal to 40 and then it's
going to cancel that log X..
So let's run do math
and they get 40 printed out.
So inside of this function in this scope we would call it x is equal to 40.
However outside of the function if I try and access X what do you expect to happen.
I get an error it tells me x is not defined.
So what this shows us is that we have two different scopes inside of this function is one context.
X is defined in here but outside of that and what we would call the global scope we're not inside any
function.
When I type X I get an error because it doesn't know what X is.
It's not visible in this scope.
So if we do define x for x equals let's just call this low.
Now if I access X when I'm outside of this function just in the global scope I get.
Hello.
So in this context X is hello.
But if I run do math again what do you expect to happen this time.
It's still 40 inside of this context.
So that's really important.
When we create a function it has its own scope its own set of variables.
That doesn't mean though that instead of a function we can't access variables that are defined outside
of the function.
So I'll show you an example.
I'm going to clear everything here and this time let's use a variable Y variable y is equal to ninety
nine.
So in the global scope I can access y.
Now if I declare a function Let's call this do more math
if I want to cancel that log.
Why do you think will have an error.
Or will this work just fine.
Let's try it do more math with parentheses.
And that gives me 99.
So what this shows us here is that when we define something outside of a function we still have access
to it inside of that function.
Think of this as a child scope.
This is more specific it's drilling down into a deeper level and child scopes have access to things
to find in the parent scope.
So if that analogy helps you go ahead and run with it.
But basically to sum it up if we define a variable outside of function we can use it inside the function
.
But the opposite is not true.
So here's another example.
What if inside of do more math I actually change the value of y.
So I say why is equal to 100.
And then I print y.
What do you think happens in this case.
That's a try.
So let's look at what y is right now.
It's ninety nine.
And if I do more math
it prints out 100.
So inside the function y is on hundred and it printed out 100.
But how about outside the function.
What happens if I just type Y.
It's also 100.
So what happened here is I declared y first as '99 outside the function in the global scope.
And then inside the function I changed y.
So this didn't declare a new variable y in the scope it actually found the old one that was declared
up here and it changed it.
So in here.
Why is one hundred and outside Why is also 100.
So here's one more example let's clear all of this.
Let's make a variable phrase equals high there and then let's make a function do something.
I know these names are not terribly creative and instead of do something I'm going to try changing.
I'm going to make a new VAR phrase equals goodbye.
And then on the next line I'm going to print it.
So what do you expect to happen here.
If we run do something
it print goodbye.
So what that tells us is instead of do something for A's is equal to good bye.
But outside of it what is phrase equal to it's still high there.
So by adding far here var phrase equals goodbye.
That makes us a new variable rather than using the one defined up here.
It makes new phrase variable that exists only in this scope only inside of do something is phrased goodbye
.
So inside it's goodbye outside we get Hi there