I don't think I hardly ever would take advantage of variable hoisting in this way:
x=2;// later:varx;
I agree that's problematic. But I don't want the language to complain at me, I want a configurable linter to complain at me. That's my objection to these errors. The language doesn't need to be my nanny. I am perfectly capable of choosing what rules I want enforced or not. And if it's a configurable and extensible tool, I can extend it to understand more nuances to when I want to allow disallow things. Once you bake an error enforcement into the language, you force that opinion on everyone, forever.
Moreover, there's many other ways that hoisting (and var) ARE helpful. For example:
Here, the try..catch construct is an "accidental" scope... IOW, I never intended it to be a scope. If I use let inside it, it becomes a scope when I don't want it to be. If I use let outside the block statement, I have to separate my declarations from my initial assignments, which I rarely like to do.
Another example:
do{varx=whatever(42);}while(x<100);
I generally prefer the loop to be treated like its own scope, but unfortunately, the while condition is not inside that scope, so I need the x to be in an outer scope. But it doesn't make any sense semantically to declare it there, since it belongs to the loop.
And also "function hoisting" is useful, for example in cases like this:
There are a variety of other edge cases and uses. But in summary: there are perfectly valid uses of var (or hoisting in general) that are not just stylistic preference or semantic in nature, but also functionally more desired.
We should use var when it's helpful, use let when it's helpful, and (occasionally) use const if we really must.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I don't think I hardly ever would take advantage of variable hoisting in this way:
I agree that's problematic. But I don't want the language to complain at me, I want a configurable linter to complain at me. That's my objection to these errors. The language doesn't need to be my nanny. I am perfectly capable of choosing what rules I want enforced or not. And if it's a configurable and extensible tool, I can extend it to understand more nuances to when I want to allow disallow things. Once you bake an error enforcement into the language, you force that opinion on everyone, forever.
Moreover, there's many other ways that hoisting (and var) ARE helpful. For example:
Here, the
try..catch
construct is an "accidental" scope... IOW, I never intended it to be a scope. If I uselet
inside it, it becomes a scope when I don't want it to be. If I uselet
outside the block statement, I have to separate my declarations from my initial assignments, which I rarely like to do.Another example:
I generally prefer the loop to be treated like its own scope, but unfortunately, the
while
condition is not inside that scope, so I need thex
to be in an outer scope. But it doesn't make any sense semantically to declare it there, since it belongs to the loop.And also "function hoisting" is useful, for example in cases like this:
There are a variety of other edge cases and uses. But in summary: there are perfectly valid uses of
var
(or hoisting in general) that are not just stylistic preference or semantic in nature, but also functionally more desired.We should use
var
when it's helpful, uselet
when it's helpful, and (occasionally) useconst
if we really must.