Lesson 9 and 10 for(var x in nyc)


#1

please i need more explanation on these two codes
i used for (var x in nyc)
in the first one i got the property keys printed out
whereas in the second one (var x in nyc) printed the values
what is different?

// first code from lesson 9
var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};

for(var x in nyc){console.log(x);}

/* result
fullName
mayor
population
boroughs
*/

// second code from lesson 10
var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};

// write a for-in loop to print the value of nyc's properties
for(var x in nyc){console.log(nyc[x]);

};
result;
New York City
Bill de Blasio
8000000
5 */


#2

@reefoo in the first loop, you are printing x, which takes the value of each individual property as it loops through (fullName, mayor, etc.). In the second loop, you are printing the value of the property that x represents -- so if x is fullName, in the first loop it would print "fullName" but in the second loop it would print "New York City."


#3

great;

thank u very much