10. List ALL the Properties!


#1



Code works perfect, but why? Is loop checking every property by itself?


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


#2

the for in loop is doing this? x gets in turn assigned each property of the object, that is the whole purpose of the loop?


#3

I thought only in Python it's possible, I understand in th lesson 9
var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};

for(var property in nyc) {
console.log(property);
}
Seems like program just check x in every property without any var x = ...


#4

you still have a question or you understand?


#5

I do, I was confused by the example in the lesson description. Thank you!


#6

property gets assigned (in turn) the property's inside your nyc, if you are familiar with python the for in loop shouldn't be new? However, in python you can use it for list and strings, in javascript you use it for objects.

The loop is doing everything for you, looping over the property's in your object, assign the property to property, so you can print it or use it to get the associated value.


#7

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