9. Getting IN-timate, problem with understanding


#1


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

for(var prop in nyc) {
console.log(prop)
}


Hello, the code acts correctly, but i don;t know the mechanics behind it.
How is the random variable (this time its 'prop') connected to the object, so it prints it's properties?


Replace this line with your code.


#2

because of the for in loop? the for in loop will assign properties to prop (the for in loop takes cares of this for you)

we could make a comparison to an array:

an_array = ["one","two","three"];

for (i = 0; i < an_array.length; i++){
   prop = an_array[i]
   console.log(prop);
}

this is basically what the for in loop does for you, it automatically detects the length, loops over it, and assigns properties to prop (or whatever you name this variable


#3

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