List ALL the properties! Code works but I don't understand why


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/zh/courses/objects-ii/1/5?curriculum_id=506324b3a7dffd00020bf661#

<In what way does your code behave incorrectly? Include ALL error messages.>

My code passes and prints out the values of the nyc object, but I never assigned anything to x. Why does this work?

```

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);
};

<do not remove the three backticks above>

#2

The var x selects all the properties.


#3

@mthfrnd,

+++++ nyc object and the for-in loop explained ++++++++++++++++

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it’s associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,

  • a fullName property with property-key fullName and it’s associated string value of “New York City”
  • a mayor property with property-key mayor and it’s associated string value of “Bill de Blasio”
  • a population property with property-key population and it’s associated number value 8000000
  • a boroughs property with property-key boroughs and it’s associated number value 5
for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration’s
iteration-1 var x = “fullName”;
iteration-2 var x = “mayor”;
iteration-3 var x = “population”;
iteration-4 var x = “boroughs”;
As they want you to display the property-key and NOT it’s associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );


#4

Thank you!! That makes sense. I think I get it now.