This rule treats variables which are defined with var declarations as the same behavior as variables which are defined with let declarations.
Please turn no-redeclare rule off if you use this rule.
This rule aims to flag below about variables which are defined with var declaration:
- References from outside of the block which declare the variable.
- Re-declarations in a same block.
- Shadowing in a same function scope.
{
var a = 0;
}
console.log(a); // not defined.for (var a = 0;;) {
}
console.log(a); // not defined.var a = 0;
var a = 0; // already defined.for (var a = 0;;) {
var a = 0; // already defined.
}function foo(a) {
var a = 0; // already defined.
}var a = 0;
{
var a = 0; // already defined in the upper scope.
}function foo(a) {
if (Math.random() < 0.5) {
var a = 0; // already defined in the upper scope.
}
}if (Math.random() < 0.5) {
var a = 0;
console.log(a);
}
else {
var a = 1;
console.log(a);
}