This lesson we're going to be looking at function scope, so go global scope of variables and values
and local scope of variables and values.
So they're really the difference between the scope is where the value of the variable is declared.
So you see in this case that we've got a variable rate and we've called it Tarlow and then we've got
variable B what this what this one is only living within the function so we can console logout E plus
B while we're in the function.
So we invoke the function and everything is great.
We'll see.
Hello World.
But then when we do console log B and this is trying to get a variable that lives within the function
and we're not able to access it because we see B is not defined.
So the thing that is happening here is this is having to do with the scope of the variables.
So the way that variables work and within functions, you can access the value only within the block
of code or its parent.
So if this particular variable is not declared and we've got our root scope here, so the AI is on the
root and we're trying to look for B on the root as well.
And the reason we're not able to find B is that it's not on the root and it's not on the parent and
the root doesn't have a parent.
So that's why we're not able to find any value for B, whereas within the function we look for B and
then we've got Consolo A plus B, so JavaScript first looks for this block of code within the function
and it sees no end there.
So it automatically reverts back to the parent and it looks for that same variable being defined within
the parent.
And if we had another parent and another parent, it would continuously look into the parents until
it actually found that variable.
And if the variable was not there, then it would still show us that undefined.
So that's how the scope works.
And I'll give you a quick example of that and the way that that can work.
So if we set up a variable and we've got a value of five, four, eight, and then within test, we
can console log out a and then we invoke test, you can see that the value of A gets placed within the
function.
Now what happens if we take a and if we redeclared it only within the function itself.
And remember, we're still outputting a within the console.
So this actually leads to two different A's.
And as mentioned earlier, the way that JavaScript will work is it will look for a within the current
block.
And if it finds it, then it will use this value of a.
So even if we try to get a value of a before we declare it, we see that it's not declared because it
doesn't go to the parent.
It knows that we've got a value of a that is within this block, but we know that it isn't declared
first.
So we need to declare it first before we try to use it.
And that's why we're throwing that error.
So if we comment that out, then what happens?
JavaScript goes through, invokes the function and sees that there's no E available within this block,
a code that's being declared and it will revert back to the parent.
So go ahead and try this out.
And it does take some getting used to so you can try to declare various variables, declare some variables
within a function and then use them within your code.