This course has been updated! We now recommend you take the Deep JavaScript Foundations, v3 course.
Transcript from the "Block Scope in ES6" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Kyle Simpson: Back to our let keyword. The let keyword, as I was saying, the let keyword is kind of like var. It will declare a variable, but it will attach that variable implicitly to whatever block it appears in rather than attaching it to the function. You recall from our discussion of the compiler, wherever it finds the var it's going to attach it to whatever function scope it's in.
[00:00:22] Well now, if it finds a let somewhere, it's gonna attach that variable to whatever block it's in, so essentially, implicitly hijacking our blocks. Here's one example, and this is probably the most common reason why people use block scoping. Anybody do for var i? You put your little variable declaration of i right inside the for-loop?
[00:00:40] You ever wondered why we do that? And I'd suggest to you, I think the reason that became so common to us is the i value there is something that we only intend to use for the for-loop. So, there's something natural about saying, if I only intend to use it for the for-loop, let me put it directly on the for-loop because, stylistically, I'm basically saying I'm not gonna use it anywhere else.
[00:01:03] Okay. Now, in JavaScript we have the ability to abuse and reuse variables all over the place, so we do it anyway. But we put the var there because we're trying to signal to ourselves and to future developers that i is only for the for-loop. Okay, but a var i here, if that had said var i on line 3, then it would have attached to the function because we know how variable declarations, they attach to their function scopes.
[00:01:28] If we do, let i, it will make that i actually attach to the for-loop as opposed to the function. It will hijack the scope of the for-loop. The same is true if we put a let inside of an if statement, like we see on line 3. The let baz = bar, that's gonna attach the baz variable to the if statement from line 2.
[00:01:49] And that's why on line 9, when we try to look at that variable, it doesn't exist cuz it's outside of its scope. So the let keyword will hijack, implicitly, the scope of whatever block it appears in. Okay? That's basically any two pairs of curly braces, although not entirely.
[00:02:10] But basically any two pairs of curly braces, the let keyword will just hijack that block. Now, this is incredibly powerful cuz it allows us to put variable declarations, following the principle of least disclosure, put them as close as possible to where they're gonna be used, as far down as is necessary.
[00:02:26] So there isn't any possible chance that they're conflicting with other variables, it allows us variable-reuse in different places of a function, and so forth. A lot of power to the stylistic thing. We've been doing this before, probably some of you have code where you put your vars inside of your ifs because stylistically we wanted to do it.
[00:02:43] We now have a way to enforce functionally what we were already doing stylistically. So there's value there, there's value in terms of early errors if we're accidentally referencing variables that we shouldn't. There's some potential performance benefits because now these values can be garbage collected earlier, since they're inside of blocks that go away.
[00:03:03] So there's lots of possible functional benefits to go along with the stylistic choice of block scoping. Does that mean that we're gonna do all block scoping? I don't think so. There are some notable people on the TC39 committee that have literally said, let is the new var. And they've encouraged people to do a global find and replace, and replace all occurrences of var with let.
[00:03:28] In fact, anecdotally, a few years ago I worked at Mozilla. Mozilla's had the let keyword for like a decade. So I worked at Mozilla and we wrote JavaScript code for the Firefox browser. And the first two code commits that I tried to get into the code base were rejected because I used vars instead of lets, and they had that culture that said because we have the let, you should always use let.
[00:03:52] I don't agree with that and maybe that's one of the reasons I don't work there anymore, but nonetheless some people think let's the new var, you should replace them all. I think you're gonna use both. I think you're gonna continue to use var declarations for things that you want to make available to a whole function, and lets for things like for-loops, where you want to put it in one specific location.