You should always use var
, if you want the value to be local.
Using the keyword object
for a variable, is not recommended, you might run into undefined behavior across browsers.
Also you should generally avoid applying anything that is suppose to be local to the global scope.
This is bad:
for (varName in object) {
alert(varName + " is" + object[varName])
}
This is correct:
for (var varName in object) {
alert(varName + " is" + object[varName])
}
If you need to access this value in the global scope, you are probably doing it wrong. Also having this in the global scope is useless, as it will only be the last value, that will exist in the varName.