Syntax to access object properties

Syntax to access object properties
0

#1

Please see the following code:

let foo = {
bar: {
a: 5
}
};

alert(foo.bar); //I get [object Object]

for (i in foo) {
alert(foo.i); //I get undefined
};

Could someone explain why? Where does the difference lie there?


#2

Your access to the object is invalid. Try:

for (i in foo) {
alert(i); //
};

#3

Rather, perhaps (as you’re trying to iterate over foo.bar, try:

for (i in foo.bar) {
alert(i); //
};