creating a new variable like this:
a=42;
should result in a let instead of a var.
e.g
for (prop in user) {console.log("no problem of scope and very helpful");
}
It seems to me like this would break existing code... currently, a = 42;
when there's no a
in scope creates a variable in the global scope. So, it's likely there's code on the web that relies on that behaviour. For example, the following code snippet would change its behaviour:
function foo() {
a = 42;
}
function bar() {
console.log(a);
}
Currently, this would log 42. With that change, it would log undefined.
An assignment to a new variable without a declaration should throw, not implicitly create a new variable in any scope.
And it does, in strict mode!
"use strict";
for (prop in user) {
console.log("no problem of scope and very helpful");
}
-> Uncaught ReferenceError: prop is not defined
The solution here is indeed to use a linter to enforce the last decades of best practices of being explicit and always including a declaration keyword.
The fact that "short assignment" behaves the way it does is an example of technical debt. ES/JS cannot change this easily, because a lot of legacy code (and minifiers) depends on this behavior.
One of the only ways would be to have a "heads-up announcement/alert" that notifies all devs and websites of future breaking-changes, and have a transition period to give time for everyone to prepare. Unfortunately, it's unlikely that will ever happen