Open your examples folder and then open the private scope sub folder. Let's look at private scope one. This is pretty much what we had in the last global example, we have a global variable called speed. And we have this function called foo, but we're accessing the global speed inside of that function. So we wind up with 25 in our console twice. Let's go to show examples.
Private scope one, and we want up with 25 in the console twice. Okay. So when you go to private scope two, you'll see that things are a little bit different. I have my global speed here. But then inside of this function, I have a private variable. And a private variable is created by using the var keyword inside of a function.
So when we use the var keyword to define a variable inside of a function, that variable becomes private at that function. So this version of speed is not available outside of this function. I want to execute line number eight, I get the private version of speed, this speed Here in line number eight references this speed on line number six, whereas this speed on line number 11 References The global speed on line number three, so I should get in my console, the global speed is 25. And then I should see the load the local speed is 100. And let's show examples private school to, and it's exactly what a global speed is 25 private speeds 100. So the point here is that now we're having we're creating a private variable, also known as a local variable, by using the var keyword.
Let's look at private scope three. So here, I have an example of lexical scope. So with lexical scope functions can see out but not in. So here on line number eight, this function bar, I access the speed variable, but this speed variable resolves to this speed. So when I see this console log statement on line number nine, it's going to say bar 100. And that's because this function bar can see outside of itself, it can see that the variable speed that didn't first variable speed of finds is equal to 100.
So here bar is executed. And this console log statement finds the version of speed is equal to 100. Also, I output a console log on line 13 referencing speed, it also lines up with the version of speed equals 100. Because again, inside of this function, speed is equal to 100. This function can see out outside of itself, and its console log statement is inside of the scope of foo. So both the bar and foo console log statements will output 100.
And then this console log statement will output 25 because here, speed resolves to 25, the global version of speed. So let's look at example number three for private scope. And we see global 25 Bar 150 100. So if we look at the last private scope example A little bit different because here, inside the bar function, we're declaring a private variable speed. So on line 11, this speed resolves to this speed, which is 500. So bar now has its own private version of speed, which means that this version is being referenced on line 15 is going to be 100.
Because we're accessing the first instance of speed we can find in foo, and the private version of speed and foods equal to 100. But in bar, the private version of speed is equal to 500. And that's because we use the var keyword to define speed which creates a private version of speed. So now we should have here global 25 and then we execute foo. We're going to one up with bar 500 and foo 100. So let's look at show examples.
Private scope, example four. That's what we get. We get global 25 Bar 500 and foo one So the main thing here that I just want to kind of point out is that tool two things. One, when we use the var keyword, we create a private variable, private or local to that function. And also that functions can see outside of themselves, that's called lexical scope. So this bar function, if we were to take away the var keyword take away this line, this would output 100, instead of 500, because it would, it would say, Hey, I don't have a private variable called speed.
Let me see if I can find one, and it would look outside of itself, and it would look to this scope and find this version of speed. But here since we declare a private variable called speed, this speed resolves to 500 and the speed resolves to 100. And then this resolves to 25 because with lexical scope functions can see out but they cannot see in and once again, the var keyword creates a private or local variable. Just for the function in which it was defined