Complex Data

Unlike primitives, objects can store multiple values as " key: value " pairs.

Creation

let user = {
  name: "John",
  age: 30,
  "likes birds": true // multi-word key must be in quotes
};

Accessing Properties

  • Using dot notation: user.name
  • Using square brackets (for complex keys): user["likes birds"]

Deleting a Property

delete user.age;
Tip

You can use variables as property names (computed properties):

let fruit = prompt("Which fruit to buy?", "apple");
let bag = { [fruit]: 5 }; // the property name is taken from the fruit variable