10. List ALL the Properties!


Code works - I'm just curious why/how the var x returns all the values when we don't assign any values to var x?


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) {


for (var x in nyc) assigns var x in nyc. The "in" part makes it so that x = any of the variables you assigned to it in the Object in the first place. Making this a for loop cycles through each x wherein x = variables in nyx.
Then when you console.log(nyc[x]) what you're saying here is

print to the console the value of the variables in NYC. THe reason it prints them all out without any issue is because it doesn't stop printing them until they're all listed out.

for (every variable in the Object nyc) print out the values of x (and again because of the for loop x changes to whatever variable it needs as it cycles through).

Hope this made sense. :stuck_out_tongue:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.