From http://www.updrift.com/article/to-var-or-not-to-var-my-javascript
- For global variables, it doesn’t matter, but you may want to use it for consistency.
- Always try to use ‘var’ to declare variables in local functions. It makes sure you’re using a local copy of the variable instead of another variable of the same name in a different scope.
For example, the two similar functions here have very different effects:
var myvar = 0;
function affectsGlobalVar(i){
myvar = i;
}
function doesNotAffectGlobalVar(i){
var myvar = i;
}