This course has been updated! We now recommend you take the Deep JavaScript Foundations, v3 course.

Check out a free preview of the full Deep JavaScript Foundations course:
The "Challenge 2: Scoping" Lesson is part of the full, Deep JavaScript Foundations course featured in this preview video. Here's what you'd learn in this lesson:

Before delivering the next Challenge, Kyle takes a moment to review scope material discussed so far in the course. In this challenge, students take what they have learned about naming function expressions and using block scope and update the simple application code.

Get Unlimited Access Now

Transcript from the "Challenge 2: Scoping" Lesson

[00:00:00]
>> Kyle Simpson: Let's check some understanding. What type of scoping rule, or rules, does JavaScript have?
>> Speaker 2: Function scope and lexical scope, right?
>> Kyle Simpson: Lexical scope, that's the model, there's only one. Function scope is the same thing as lexical scope. Dynamic scope doesn't actually exist in JavaScript, but it does exist in a few esoteric languages, okay?

[00:00:18] So there's just lexical scope. What are three different ways that you can create a new scoped variable?
>> Speaker 2: Put a let, or-
>> Kyle Simpson: You can do a let, that would create a new variable inside of some scope.
>> Speaker 2: Var.
>> Kyle Simpson: Var inside of a function, like we can make an iffy or a function and make a var inside of it.

[00:00:46]
>> Kyle Simpson: [CROSSTALK] There one is tricky. That's the same thing, passing in an argument to an iffy.
>> Speaker 2: Function declaration.
>> Kyle Simpson: Same thing, we have a parameter list that's the same thing as a var declaration.
>> Kyle Simpson: How about the catch clause? The error in the catch clause creates a new variable.

[00:01:06]
>> Kyle Simpson: Let's review from earlier in the course. What is the difference between undeclared and undefined, now that we fully understand lexical scope? What's the difference between undeclared and undefined?
>> Speaker 2: Undeclared, it's never been declared.
>> Kyle Simpson: It's never been declared in any scope we have access to, exactly. What about undefined?

[00:01:27]
>> Speaker 2: It's not been declared but it doesn't [CROSSTALK]
>> Kyle Simpson: It has been declared in the scope.
>> Speaker 2: It doesn't currently have a value.
>> Kyle Simpson: But does not currently have a value. Right, does not currently have a value. All right. All right, exercise #2, now, read the read me, cuz it describes to you exactly what to do, don't skip over that detail.

[00:01:45] But in exercise #2, your task is to take what we've learned about naming our function expressions and using block scoping. Take what you know now about lexical scope and improve the code. So we're not adding any new features, we're just improving the code by usage of block scoping, creating explicit scopes, that sort of stuff, okay?

[00:02:04] Shouldn't be more than about five or eight lines of code that you'll write or change to do this, so we'll give you about five minutes to work on the exercise, and then talk through the solution. I wanna point out a little resource that may not have been obvious if you looked at the file system.

[00:02:19] But in the exercise folder, not only do I provide the solution file for each of these exercises, which by the way, there's not always only one right way, okay? So if yours differs from it, don't feel weird or whatever. There's multiple ways to approach it, this just represents one good way of going about it.

[00:02:38] But in addition to that, what I also provided was a diff between the exercise file and the solution file in each of those directories, it's called diff.txt. And that's just the side by side diff output from comparing those two files. So for example, if I open up the diff here, you'll notice, and of course, I need to turn off my word wrap here.

[00:03:05] You notice that I have a side-by-side diff going. It might be easier to look at this in VI or in the terminal or something like that. Seems like my spacing and my editor is going weird here. I don't know why my editor is not looking at things correctly.

[00:03:22] Look at it in your console, [COUGH] in VI or something like that, or count the output of it. But that should be a resource for you to be able to look and check to make sure that you addressed each of the things that I addressed, so.