JSHint complains if I have multiple for loops declaring the same index variable:
for(var i=0; i<10; i++){
console.log(i);
}
for(var i=0; i<10; i++){ //<-- jshint warns that 'i' is already defined
console.log(i);
}
Is there a way to turn this warning off? I couldn't find any when I searched...
The reason I want to do this is that I prefer keeping my index variables declared together with the loops instead of hoisting the declarations to the top of the function. I think repeating the declarations is more robust if I delete the for loops or move them around and I also think it helps convey the intention that the loop variables should not be used outside the loops.
JSHint complains if I have multiple for loops declaring the same index variable:
for(var i=0; i<10; i++){
console.log(i);
}
for(var i=0; i<10; i++){ //<-- jshint warns that 'i' is already defined
console.log(i);
}
Is there a way to turn this warning off? I couldn't find any when I searched...
The reason I want to do this is that I prefer keeping my index variables declared together with the loops instead of hoisting the declarations to the top of the function. I think repeating the declarations is more robust if I delete the for loops or move them around and I also think it helps convey the intention that the loop variables should not be used outside the loops.
Share Improve this question edited Aug 3, 2014 at 15:48 hugomg asked Aug 3, 2014 at 6:31 hugomghugomg 69.9k29 gold badges164 silver badges254 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 20The shadow
option disables this warning.
/* jshint shadow:true */
for(var i=0; i<10; i++){ console.log(i); }
for(var i=0; i<10; i++){ console.log(i); }
var i
to the top of the function like the warning is suggesting me then jshint will let me nestfor(i=
loops inside one another without giving any warnings either. – hugomg Commented Aug 3, 2014 at 6:42for
loop within a function. Then you'd silence jsHint and be free to move the loops wherever you want in that function without breaking anything, including even nesting them inside another loop. Note that ES6 intends to solve this withlet
used in afor
loop initializer. – jfriend00 Commented Aug 3, 2014 at 7:29