This course has been updated! We now recommend you take the Complete Intro to Web Development, v2 course.

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

When you declare a variable in JavaScript, the scope of that variable refers to where it was declared. This is important because a variable is only accessible inside its scope. For example, if you declare a variable inside a function, it can only be used inside that function. Nina demonstrates how this works and why it’s important to always use the var keyword when declaring variables.

Get Unlimited Access Now

Transcript from the "JavaScript Scope" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Nina Zakharenko: So the last thing that we're gonna talk about today is scope and scope means where and how you can use the variables you've declared. So if you define a variable in a function, it's only visible to that function. If you declare a variable outside of the function, and that function is in that same file or space, then you can use that variable in your functions.

[00:00:33] So let's talk about what happens when you forget to use var.
>> Nina Zakharenko: So
>> Nina Zakharenko: We have two functions here. Print fruit and a print veggie. Each declare is a variable inside of them. And then, writes that variable. If we do print fruit and print veggie, everything is super happy, we get the results we expect.

[00:01:06]
>> Nina Zakharenko: Let's do this quick exercise. We're gonna copy the document.write that's in fruit into the bottom of your veggie function and just run the code as is. Can someone tell me what the result of that is?
>> Nina Zakharenko: So, nada.
>> Speaker 2: Yeah.
>> Nina Zakharenko: Veggie doesn't know what a fruit is.

[00:02:01] So we're gonna do something tricky and remove this var in front of the fruit. So now it's popping up. And the reason is because if you declare a variable in JavaScript without using var, it plops it on the global scope. It's like, hey, anyone can use me, read me, change me, do whatever.

[00:02:25] And that's a pretty bad thing. It can happen by mistake. If you accidentally forget the var and then reuse the variable name. So don't do it. Try to be very careful about using var and not really populating, or polluting the global scope with different variables. So even if you have a good use case, you know maybe you do want a variable used across all sorts of functions.

[00:02:58] Try not to do it. It's a really bad practice.
>> Nina Zakharenko: A good rule of thumb about scopes is this little squiggly brackets. If things are nested, then it can probably access the parent variables. But if things are kinda of the same level like this printFruit and this printVeggie, then they probably can't access each other.

[00:03:27] And that's functions and classes but does not apply to if or for statements, for example.
>> Nina Zakharenko: This make sense to everyone? This is kind of something that's kind of tricky, yeah.