Transcript from the "Scope" Lesson
>> Speaker 2: And do you have another course on that?
>> Kyle SImpson: There is a course on ES6, so.
>> Speaker 2: You're asking about a function callback.
>> Kyle SImpson: That's more than we'll get into right now. We'll probably come back to that after lunch. But that's more than we'll get into now. What I wanna do in our last couple minutes is talk about the difference between this variable a which exists outside of the functions, and this variable b which exists only inside of the function.
[00:00:44] You'll notice I have another variable called b here, and these bs are entirely different. Does everybody see that? There's a variable b, it's almost like there was a var in here. There isn't, but it's almost like there was a var in here, that declarative b that exists only inside of that function.
[00:00:59] So if I took this part out, and I just tried to access b, what we expect to have happen is an error because there is no b available to line 9.
>> Kyle SImpson: The b exists only inside of the function, not outside of it. Identically, too, if I said var c = 42, if I put a variable inside of my function, it's not available outside.
[00:01:30] And if I said var c, if I take that program and I run it, we're gonna expect to get a reference error because c doesn't exist. And in fact, we get a reference error that c is not defined, okay?
>> Kyle SImpson: So putting a variable outside versus inside a function changes where it is scoped.
[00:01:59] Scope is this technical term for, you can think of it as sort of there's a bubble around the internal contents of foo, and then there's another bubble around the outer contents of our whole program. Those are two different scopes. There's one scope here and one scope here. Notice that there's no a inside of our foo, and yet we didn't get a problem.
[00:02:21] And the reason for that is that you can always access variables above you or outside of your scope. But you can never reference variables that are further down. So that's a one-way street. When I ask for a here, it first looks to see if it sees an a inside of foo, but it doesn't.
[00:02:41] And so where it goes next is it goes out one level of scope. And it finds an a there, so it's able to use it. But the reverse was not true. We could not take a variable that was inside of it and access it from the outside.