10. List all properties (Question about the loop)


Alright so I finished the section, but I'm not fully certain why it behaves the way it does. This is the code:

var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5

var x = "fullName"
// write a for-in loop to print the value of nyc's properties
for (var x in nyc) {

So I mark "fullName" as variable X, and I ask the loop to list variable x (which, again, is nyc.fullName). How come it logs all the property values? Why doesn't it just list the value of fullName?


Assign each property name of nyc into the name x

Previous values of x has no effect on that.
If you want to look up a particular property, then you have no use for a loop. Just look up the property directly.

Say you have a English to Japanese dictionary, would you compare each word in the dictionary to the word you are looking for, or would you look it up by finding the correct location in the book where you know your word is because it's alphabetically sorted? Same thing.


I assign a unidentified var i.e. var x = nyc[x]
I think this is the most correct because your for in function is going to fill in the value of x with the property it finds and spit out the value. So you don't have to spend the time involved loading all the properties into one variable. this produced the answer for this question, what do y'all think?