JavaScript Var vs Let
The cluster focuses on discussions about the scoping differences between function-scoped 'var' and block-scoped 'let'/'const' in JavaScript, including debates on the benefits of block scoping, reasons to avoid 'var', and examples of scoping issues in loops and blocks.
Activity Over Time
Top Contributors
Keywords
Sample Comments
new scoping for `var`? Like the block-scoped `let`?
... because var is function-scoped, not block-scoped like let.
How is having var be function scoped instead of block scoped a useful feature?
No, because you have to use the new 'let' keyword to get block scoping. So you have to opt-in to it.
You want block scoping. Block scoping is coming in ES6 with the let keyword.
`var` is function scoped, `let` is block scoped.
It's the same reason that, yes, you can put "var x" in an if-block, but it's bad practice because it misleads the reader (even one who knows that Javascript is function scoped) into thinking that x is block-scoped.
Err, sounds like a weird thing to do, and as they mentioned, is more likely just a limitation of their inferrence engine. Preventing variable shadowing leaks implementation details from inside functions/blocks and mos tlanguages do just fine with appropriate warnings and so on.
How about don't use let or var declarations at all?
the one thing that annoys me about let/const that leads to me occasionally using var is that with var you can declare a variable in a try block and then use it afterwards, instead I have to declare a let outside the try or put way more then I want to in the try.