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

Kyle presents a use case for an explicit let block.

Get Unlimited Access Now

Transcript from the "Explicit let Block" Lesson

[00:00:00]
>> Kyle: While we're on the topic of using block scoping, and while we're on the topic of considering whether or not lets at the top level of your functions are appropriate. If you're of the habit, as many of you are, of using lets at the top level of your function, and you still want to do that.

[00:00:16] I'm going to caution you also that there are lots of times where you're only going to use a variable for a few lines of code, and you should not just throw it into the top level of the scope. What you should do is create a scope for it.

[00:00:30] Open up a curly brace pair and create a scope just for those three lines of code to use the prefixed and the rest variable, and then don't let them exist for any other part of the function. Don't just say let, and prefix, and rest at the top of your function, open up a curly brace there.

[00:00:47] As a matter of fact, I do that anywhere, even inside of an if statement. If I have a variable that only needs to exist for a couple of lines, the way to do that is to make a block for it. And that's not a new invention of mine, by the way.

[00:00:59] We've done that for 30 years in every other programming language that's ever had block scoping. We open up curly brace pairs and we scope it to the block. All I'm saying is let's do that in JavaScript because it worked really well in other languages, yes?
>> Speaker 2: Are you making this argument purely for semantics, or is there a performance benefit to doing something like that?.

[00:01:20]
>> Kyle: It is extremely unlikely that you would ever see or be able to observe any performance difference. There are theoretical performance differences, like theoretically an offthread garbage collector could theoretically garbage collect it slightly earlier. Or theoretically it would reduce what was available to a closure. There are theoretic reasons, but in practice, you almost certainly would not see a performance difference this way.

[00:01:50]
>> Kyle: I'm simply saying that this communicates more clearly. And by the way, I intentionally did put my lets, it's not just saving space on the slides, it helps. But I intentionally do, if I'm gonna open up a block, just for the existence of a couple of variables like that, I'll put my declarations on the very same line as the opening curly brace.

[00:02:09] To make it super clear to the reader, hey, these two are created for this block, and they exist only for the purpose of the block. As opposed to, you have to go looking for whether or not there are variables in this scope. You have to go figure it out.

[00:02:24] I want it to be super obvious.
>> Kyle: I strongly recommend that you consider using more of these explicit let blocks, narrowing the scope where you are using your variables as much as is practical.