Any variable which is not declared inside a function, is treated as a window variable:
for (xxi = 0; xxi < 10; xxi++) {
console.log(xxi); // this variable
iix = 99; // this variable
}
If you had done this, javascript won't read the variable outside the scope:
for (var xxi = 0; xxi < 10; xxi++) {
console.log(xxi);
var iix = 99;
}
Explained (Let's understand using declarations instead of looking for errors):
var xxi = "one"; //global variable
var iix = "two"; //global variable
globalVar = "three"; // window variable i.e. window.globalVar
var getValues = function () {
var xxi; //local variable for the scope
var iix; //local variable for the scope
for (xxi = 0; xxi < 10; xxi++) {
console.log(xxi); // takes the local variable
iix = 99; //local change of value
}
globalVar = 100; //change of value to window variable accessible inside
};
getValues();
var seeValues = function () {
console.log(xxi); //"one" //Displays the value of global because the local variable of getValues() is not accessible outside
console.log(iix); //"two" //Same as above
console.log(globalVar); //100 and not "three" //Displays the altered window variable
};
seeValues();
See this link for detailed information: http://www.dotnettricks.com/learn/javascript/understanding-local-and-global-variables-in-javascript and What's the difference between a global var and a window.variable in javascript?