0
var nyc = {
    fullName: "New York City",
    mayor: "Michael Bloomberg",
    population: 8000000,
    boroughs: 5
};

var myProperty = this.nyc;
/*this is one variable so how can it store all the values and what does this.nyc mean
and what value it carries, value of all property or just the value of one property*/
for(myProperty in nyc){console.log(nyc[myProperty]);}
//how is this line giving me the value of all the properties of object.

How does basically for in loop works?

1

1 Answer 1

1

myProperty is set to a new value at each iteration of the for loop. So the old value of myProperty does not matter.

2
  • In fact, the assignment on top of the loop is redundant and highly confusing. A plain var myProperty; would have been better.
    – Thilo
    Feb 16, 2012 at 7:27
  • @Thilo: I agree. I think this is a symptom of his core confusion--he does not understand why, after assigning a particular property to myProperty it can still be used to enumerate all of them. Feb 16, 2012 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.