for in loop assigns object properties to the left side variable.
Shouldn’t it be used only for ‘inside the body of loop’. I don’t know permanent assignment(obviously before completion of script) makes any sense?
Does anyone see use case?
Below script copies object properties to the array.
var obj = {a:1,b:2};
var arr = [];
var x = 0;
for(arr[x++] in obj) // shouldn't be temporary assignment?
{} // empty body
for(new_values in arr){
console.log(arr[new_values]) // i can fetch values here.
}
//a
//b
Because it isn’t restricted anywhere. JavaScript variables are nice and simple. The only scope they have is function level and then only when you use the
varkeyword.