Object inside an object, proper syntax, help


as can be seen here..


we are building steve and bill objects inside friends. and as can be seen in the link, bill and steve objects are closed with a semicolon tag.

then come to this exercise..


we need to build steve and bill inside friends. so when i am trying to use semicolon to close bill and steve object, i.e.

var friends=

i am getting syntax error..
i know that using comma lets you complete this exercise. but i would like to know the proper syntax for closing objects(or proper syntax for closing objects inside objects).

var friends=

friends is an anonymous object in an assignment statement. The syntax is correct on this part.

bill and steve are properties in object literal syntax, so are separated by commas. Their values are also anonymous objects, but the semi-colon syntax does not apply.

{ key1 : value1, key2 : value2 }

The only time we use a semi-colon after a closing brace, }; is when there is an assignment:

var obj = { key: value };

var func = function () { // code statements };

When we write,

bill: { }

we are declaring an association, not an assignment. The object is associated with the bill key.


thank you.

1- so basically, bill is a key here, which is again going to store some keys and values(like an object) since we added {}
is that correct?


That is correct. bill and steve are what I call primary keys of the friends object. Their values are both objects, which will, like all objects, also have key: value property pairs.

We may associate any primitive or object to a key:

var mixedObj = {
    number: 1,
    string: "String",
    boolean: true,
    array: [],
    object: {},
    method: function () {}


perfect. thank you.very much. this sounds like associative arrays from PHP..


JavaScript objects are very much like associative arrays in PHP, especially when we view them in subscript syntax:

var mixedObj = {};

mixedObj['number'] = 1;
mixedObj['string'] = "String";
mixedObj['boolean'] = true;
mixedObj['array'] = [];
mixedObj['object'] = {};
mixedObj['method'] = function () {};

In an IDE with an active linter, these lines will all have a caution (excepting the var statement):

... better written in dot notation

It's still valid, though, just not very common. We see this relationship in the for..in statement...

for (var key in object) {
    console.log(key + ": " + object[key]);


alright, thank you very much sir.


okay one more question here..
in associative arrays you write echo $array["key"] to show the value. how do you do the same in javascript?

consider this code..

var friends={

how do i print lastName property of bill? tested console.log(bill.lastName); and it doesn't work here.


bill.lastName is undefined, but, friends.bill.lastName is defined. We can iterate over the secondary objects (nested as values in primary properties) with a nested loop construct:

for (var key in friends){
    for (var k in friends[key]){
        console.log(k + ": " + friends[key][k]);

We won't likely see this very much, but here it is:



interesting, this is like accessing a specific value of a multidimensional array

thank you very much for all the help.