Dots and Brackets: are they the same, or not?

Tell us what’s happening:
Hi,
can anyone clarify why this does not work and I need a dot notation?

userObj[data][friends].push(friend);
return userObj[data][friends];

And maybe ponting me to where this is explained because what I understood so far is that you can use dots or brackets…

Your code so far


let user = {
name: 'Kenneth',
age: 28,
data: {
  username: 'kennethCodesAllDay',
  joinDate: 'March 26, 2016',
  organization: 'freeCodeCamp',
  friends: [
    'Sam',
    'Kira',
    'Tomo'
  ],
  location: {
    city: 'San Francisco',
    state: 'CA',
    country: 'USA'
  }
}
};

function addFriend(userObj, friend) {
// Only change code below this line
  userObj.data.friends.push(friend);
  return userObj.data.friends;
// Only change code above this line
}

console.log(addFriend(user, 'Pete'));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Modify an Array Stored in an Object

Link to the challenge:

userObj.data looks for the property named “data” on the object named “userObj”.
userObj[data] looks for the property name that matches the value of the variable data on the object named “UserObj”.

if you want to use bracket notation with the exact property names you need to use strings
so

userObj["data"]["friends"].push(friend);
return userObj["data"]["friends"];
1 Like

Great, thank you for this, much appreciated.
L