JavaScript variable scope can be particularly difficult to understand and get right.
The situation gets even worse when you consider the accidental creation of global variables,
which is what happens when you declare a variable inside a method or the for clause of a for-loop without using the var keyword.
function f(){
i = 1; // Noncompliant; i is global
for (j = 0; j < array.length; j++) { // Noncompliant; j is global now too
// ...
}
}
function f(){
var i = 1;
for (var j = 0; j < array.length; j++) {
// ...
}
}