Consider the following code:
for (var i=0; i<100; i++) {
// your code here
}
// some other code here
for (var i=0; i<500; i++) {
// custom code here
}
Any decent lint tool (jslint, jshint or built in IDE) will tell warning - duplicate declaration of variable i
. This can be solved by using variable with another name (k
, j
) or moving declaration to the top:
var i; // iterator
for (i=0; i<100; i++) {}
for (i=0; i<500; i++) {}
I am not fond of both variants - I don't make declarations at the top usually (and even if I did I wouldn't want see there helper variables - i
, j
, k
) and really nothing bad is going on in those examples to change variables' names for.
Though I do want a huge warning in case I write something like this:
for (var i=0; i<100; i++) {
for (var i=0; i<500; i++) {} // now that's bad
}
What's your approach to such cases?