22

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.

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • 2
    "I think repeating the declarations is more robust if I delete the for loops or move them around" - why? it's exactly that it will break miserably if you e. g. move one loop inside another because of the lack of block scope. – The Paramagnetic Croissant Aug 03 '14 at 06:32
  • I was thinking about when I move the loops laterally (this can be a problem if I leave the declaration in the first loop and remove it in the second one). – hugomg Aug 03 '14 at 06:35
  • Additionally, if I hoist the `var i` to the top of the function like the warning is suggesting me then jshint will let me nest `for(i=` loops inside one another without giving any warnings either. – hugomg Aug 03 '14 at 06:42
  • by not refining the variable? lol – AdityaParab Aug 03 '14 at 07:05
  • You could use a different letter for each `for` 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 with `let` used in a `for` loop initializer. – jfriend00 Aug 03 '14 at 07:29
  • possible duplicate of [Is there a way to suppress JSHint warning for one given line?](http://stackoverflow.com/questions/12977661/is-there-a-way-to-suppress-jshint-warning-for-one-given-line) – Krzysztof Safjanowski Aug 03 '14 at 10:58
  • I would vastly prefer if I could still name my loop variables `i` (like I can do in every single other language in the planet) instead of having to come up with a new name every time. Additionally, the suggestion to silence jshint completely doesnt somve the problem dor me - it adds even more clutter than moving the variable to the top of the function. – hugomg Aug 03 '14 at 15:35
  • Note that, as of version 2.5.3, jshint will still complain about variables already defined when using `let` instead of `var`, with the `esnext` option set to true. – K. P. MacGregor Mar 02 '15 at 02:28

1 Answers1

20

The 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); }
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • 2
    Unfortunately there appears to be no way to *globally* say "disable this warning only for local variables in 'pseudo-scopes'" (or maybe it is merely jshint failing to consider the initialization section of a for-loop as part of the pseudo-scope body). ie. one might prefer to run as `shadow:outer` otherwise.. – user2864740 Sep 03 '15 at 04:36