I am currently going through the javascript track on codeacademy.com.
The current lesson asked me to do this, which will print out the value of each property:
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 p in nyc){
console.log(nyc[p]);
}
All very straight-forward. But my question is, why does this not work when I use dot notation inside the for-loop? Like this:
for(var p in nyc){
console.log(nyc.p);
}
Instead of printing out the 4 property values, it prints out the word 'undefined' four times. It seems illogical to me that I shouldn't be able to access the property using the dot notation also. If this isn't some peculiarity of the codeacademy lesson, then could someone please explain this to me?