What is the correct syntax for declaring a loop-dependent variable in a for / in loop?
The first two both seem to work (and don't raise flags in the Google Closure Compiler), but only the third passes Crockford JS Lint. I do not want to use it, mainly because it is not compact.
JSLint complains that either val is a bad variable (when I don't add var ), or that the declaration needs to be moved.
Are there any disadvantages for the first or second option? What should i use? (It is assumed that str is a declared string, and vals is a declared object)
1. No declaration:
for(val in vals) { if(vals.hasOwnProperty(val)) { str += val; } }
2. In the `for 'var declaration:
for(var val in vals) { if(vals.hasOwnProperty(val)) { str += val; } }
3. Outside of the var loop declaration:
var val; for(val in vals) { if(vals.hasOwnProperty(val)) { str += val; } }
Yahel source share