ECMAScript/JavaScript "let" vs "var": much ado about nothing?

Lord Pachelbel

Ars Praefectus
5,499
Subscriptor++
[In case you don't know, in ECMAScript/JavaScript, variables declared with var have global or function-level scope, and variables declared with let have block-level scope.]

Lately all the code-quality folks and linting tools are saying "use let instead of var," but I can't think of a situation where using let instead of var makes any real difference. Plus there are plenty of situations where you have to use var.
  • Need a global variable? Then you need to use var. (Or const, if you want to prevent reassignment.)
  • Need a variable that will be modified in a loop and then used outside the loop? Then you need to use var.
  • Need to use the same variable name both inside and outside of a function while giving them different scopes? var already does that, so using let makes no difference.
  • Need to reference a variable before it's declared? Then you should probably restructure your code so that you're not doing that in the first place, because it's usually bad style.* But leaving aside questions of style, if you're relying on hoisting, then you need to use var.
  • Need to declare and initialize a variable in the middle of the function, immediately prior to using it, because it's easier to understand the code that way? Then there's no difference between using var or let.
  • Need to use the same variable name several times in a bunch of nested if-statements, and you want them all to have different scopes? Then you need to use let. BUT WHY DO YOU NEED TO DO THIS? Why can't you use different variable names? Wouldn't it be more confusing, not less, to see the same variable name declared several times?
*Sometimes the code in a file is easier to understand if, say, you reference an event handler before declaring it, because the handler's code is really really long compared to the main block of code.

I will grant that using let can help beginner programmers avoid certain kinds of bugs, but so can writing code with better style. Examples:
  • If you always declare variables before using them, then let's non-hoisting behavior is superfluous.
  • function foo() { x = 5; } — Initializing a variable inside a function without a keyword gives the variable global scope. But if you always initialize variables with var, then you can't accidentally create global variables, and so let's implicit protection against that sort of thing is superfluous.
So I would love to see a non-contrived situation where using let instead of var is actually useful. Something that could only be done by using let.

Otherwise, this is my contention:


via Imgflip Meme Generator
 

rabish12

Ars Legatus Legionis
16,983
Variable hoisting is a confusing feature that can, on occasion, lead to confusing bugs unless you structure your code in a very particular way. As you mentioned, you declare your variables at the top of the scope and then assign and use them as they're needed. This fixes the hoisting confusion var can cause, but it is not free. It massively increases the span of your variables, and it effectively forces all of your code to rely on mutation. These are reasonable trade-offs to make when you have no choice, but choosing to make them because you don't want to use a language feature that fixes the problem outright is just plain silly.

var also has somewhat inconsistent and slightly confusing behavior, and you've actually managed to demonstrate that with your post. vars that you declare "globally" are actually placed on the window object, a behavior that you can replicate by just assigning them directly (ie. "window.foo = 123"). Unless you specifically intend to monkey-patch that object (ie. to add a polyfill for a feature that's not yet supported), don't do this. It's trivially easy to clobber these values by accident, especially when you're using code from other developers who've adopted the same practice. There are always better options.

As for your for-loop example, you can declare your "let" variable outside of the loop if you need to use it outside of the loop. If you're writing var-based code appropriately then you're going to be doing this anyways, since you'll be putting that var at the top of your function. The fact that it's declared in a for loop won't prevent it from being hoisted.

That said, you've fundamentally misunderstood the point of let. Yes, you can use it to shadow variables for if/for constructs. There's also some other fancy constructs that you can do with block statements that let you avoid some closures. That's not why let is important. It's important because it makes the language work in a more predictable way and because it replaces a language feature (variable hoisting) that's dangerous and that has no real benefit. It's not there to let you write fancy new kinds of code, it's there to prevent stupid mistakes and reduce confusing language quirks. Unless you're supporting IE10 and under and you're not compiling your code, you have no reason not to use it.

Don't rely on "good style" and your own cleverness to avoid mistakes when there's a language feature that makes those mistakes impossible.
 

rabish12

Ars Legatus Legionis
16,983
Wait, what? Someone is defending global variables?
Someone is defending a language feature that automatically makes variables you declare in certain scopes into global variables because it has the ability to do that.

Seriously, var's semantics are really bad. They didn't add "let" just for the fun of it, they added it specifically to replace var with something that made more sense.
 

Ardax

Ars Legatus Legionis
19,795
Subscriptor
BUT WHY DO YOU NEED TO DO THIS? Why can't you use different variable names? Wouldn't it be more confusing, not less, to see the same variable name declared several times?
Because if the variable is block scoped, it doesn't matter if you're re-using the name. The variable's scope should be visible. Though I'd still say you've probably got a code smell of some kind going on there.

Need to declare and initialize a variable in the middle of the function, immediately prior to using it, because it's easier to understand the code that way? Then there's no difference between using var or let.
With let you know the variable can't be hoisted into the window scope. With var you need to verify that it isn't.

The point is that if you use let everywhere you can, then you know you need to examine your var declarations more closely.