Lesson 28 +10 XP

Object Properties

Object Properties

Properties are the building blocks of objects. You can add, change, and remove them.

Add a property

const person = { name: "Ada" };
person.country = "England"; // added now

Change a property

person.name = "Ada Lovelace";

Delete a property

delete person.age;

Check if a property exists

"name" in person;   // true
person.name !== undefined; // also true

List all property names

Object.keys(person); // ["name", "country"]

Computed property access

Use bracket notation with a variable when you do not know the key name in advance:

let key = "name";
person[key]; // same as person["name"]

TL;DR

  • Add and change properties by assignment.
  • delete removes a property.
  • in checks if a property exists.
  • Object.keys() lists property names.