In pragmatic terms, it's fine to reuse the same variable in your example, but by declaring it twice, you're giving the impression you think it's scoped to the for loop. It isn't, which is why JSHint is giving you that notice: To make sure that it's not actually a bug in the code.
If you want to reuse i, just declare it once:
self.aFunction = function(otherAll, all){
var i;
for(i = 0; i < all.length; i++){
...
}
for(i = 0; i < otherAll.length; i++){
...
}
return someResult;
};
Alternately, if all and otherAll are arrays, look at forEach or the many other options in my other answer here.
IF you're able to use ES2015 features (e.g., you know all your target environments support them, or you're transpiling), then:
You can use let:
self.aFunction = function(otherAll, all){
for(let i = 0; i < all.length; i++){
...
}
for(let i = 0; i < otherAll.length; i++){
...
}
return someResult;
};
The i declared with let will be scoped to the for loop (in a very interesting way, no less1).
Or if you don't need the index and just want the values in the loop, and all and otherAll are iterables (arrays are iterable), use let with the new for-of:
self.aFunction = function(otherAll, all){
for(let value of all){
...
}
for(let value of otherAll){
...
}
return someResult;
};
1 "...in a very interesting way, no less...": In for (let i = 0; i < all.length; i++) there's actually a different i for each loop iteration, which gets its value from the value of the previous i just before the increment part of the loop, which is useful if you create closures within the loop — they close over the i for that iteration:
// let...
for (let i = 0; i < 5; ++i) {
setTimeout(function() {
console.log("i = " + i);
}, i * 100);
}
// ...vs. var
setTimeout(function() {
for (var j = 0; j < 5; ++j) {
setTimeout(function() {
console.log("j = " + j);
}, j * 100);
}
}, 500);