The answer is no, in my opinion:
function doSomething( anArray ) { for (var i = 0; i < anArray.length; ++i) { if (someTest( anArray[i] )) break; } return i === anArray.length ? "test failed" : "test succeeded"; }
This is not the most beautiful thing in the world, but checking the iterator variable after the loop is at least somewhat idiomatic. It is also possible that a var i in one for loop is used in the same function in a later for loop that does not repeat var .
Now, whether code needs to be improved in such a way as to use let is another question. However, it should be clear that blindly replacing such use of var with let in the existing code base would be seriously unreasonable.
Going forward, of course, it’s a good idea to limit the scope to as limited as possible, but dogmatic rules like “always use let , not var in for loops” cannot take into account all the programming problems you may encounter. “Best practice” is “best” so far.
source share