Check out a free preview of the full Complete Intro to Web Development, v2 course:
The "Scope Solution" Lesson is part of the full, Complete Intro to Web Development, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian walks through the solution to Scope Exercise.

Get Free Access Now

Transcript from the "Scope Solution" Lesson

>> Brian Holt: Let's go ahead and go through this. So I have const A right here. This is gonna be in the global scope. So no matter where this gets logged, it's definitely gonna survive, okay. And then, I have let F. Even though, it's gonna be modified somewhere else, it's also in the global scope.

[00:00:14] So anywhere that you reference F, it's gonna survive. It's never gonna go out of scope. Okay, I got a function called doStuff that takes in B. So let's just go through this and figure out where all the console logs are. Where we have E. So let E = 0.

[00:00:31] This is also in the global scope, gonna survive. So E++; whatever console.log(A). A is in the global scope up here, this works. This one works, okay? E, E is also in the global scope, this one's gonna work. G, G is inside of the while loop, not gonna work.

[00:00:56] doStuff, I pass in the letter B. And this becomes B the parameter up here. Console.log(B), this came in from here. So this is gonna survive. So if this is gonna work. C is declared right there. H is declared. D right there. H is something else. Console.log(D); D is right here.

[00:01:22] This is inside of the if statement, this is outside of the if statement. Therefore, D is not going to work. H, H is declared here. Even though it's changed here, it was declared up here. Therefore, this is gonna work, yeah. F is changed. But that doesn't matter, it's in the global scope.

[00:01:44] So when it come down here and do console.log(B). That's the name of the parameter up here. So that's not gonna work. This one's not gonna work. C, it's declared inside of doStuff. So that's not gonna work, that's gonna be out of scope as well and F. F is declared in the global scope and is changed inside of doStuff.

[00:02:07] But because it's declared in the global scope, so it is going to work.
>> Brian Holt: So let's see how we did. This works, does not work, works, works. So you can see here, I kinda left you a bunch of notes that you can see go back and review and see what works and what doesn't work.

[00:02:30] And you can see here that it actually errors out on G. Cuz it's G, G breaks it right there.