4. Adding properties


#1

var friends = {
bill: {
firstName: 'Bill',
lastName: 'Gates',
number: '0123456789',
},
steve: {
firstName: 'Steve',
lastName: 'Jobs',
number: '101101',
},
};

SyntaxError: Unexpected identifier

Can't find the source of the problem here?


#2

You have a comma after the phone number. That's not correct. You also have a comma at the end of the associative arry. You also don't have var in front of bill or steve. And you don't enclose the two arrays in another array.

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
}

#3

@mr_bishop, I saw only syntax errors.

var friends = {
   bill: {
      firstName: 'Bill',
      lastName: 'Gates',
      number: '0123456789' // problem here -> without ,
   },
   steve: {
      firstName: 'Steve',
      lastName: 'Jobs',
      number: '101101' // here -> without ,
   } // and here -> without ,
};

@tekkie1618, @mr_bishop created an object friends which has two nested properties (are also objects) bill and steve. Now, we can access to Bill firstName property using dot notation like this:

console.log(friends.bill.firstName); // returns "Bill"

@tekkie1618, you said:

You need a semicolon.

I don't agree. Look to "Creating objects" and instruction:

When we have more than one property, they are separated by commas. The last property does not end with a comma.

Also

You also don't have var in front of bill or steve.

When creating an object, each property has a name, followed by : and then the value of that property. bill and steve are properties, so it's no necessary to add var.

Perhaps, I don't understand @tekkie1618's answer.


#4

For some reason, I thought he was just making an object. Not a nested object.