If dog.species = dog["species"] how come nyc[property] works but nyc.property does not?


#1

List ALL the Properties

In the description/explanation, it is clearly stated that:

var dog = {
species: "bulldog",
age: 3,
color: brown
};

First, remember that
dog.species = dog["species"] = "bulldog";

My question is that, if:
dog.species

is effectively the same as
dog["species"]

how come, within the for-in loop, nyc[property] != nyc.property?

Here is my code for the question:

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 property in nyc) {
    console.log(nyc.property);
};

This produces an error of:

Oops, try again. It looks like you didn't print the value of nyc's fullNameproperty.

and:

undefined
undefined
undefined
undefined

gets printed to the console.

However, when the for-in loop is changed to:

for(var property in nyc) {
    console.log(nyc[property]);
};

everything works fine. My understanding is that both should be the same, but this is not the case. Why?


#2

nyc.property

is equivalent to

nyc['property']


#3

I'm also confused by this and wondering the same thing


#4

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