Object Properties

Add, change and remove object properties.

Syntaxobj.newKey = value; delete obj.key;

You can modify objects after creating them:

  • Add or change a property by assigning to it.
  • Remove a property with delete.
  • List the keys with Object.keys() and values with Object.values().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Iterate over a settings object to display all key-value pairs in an admin panel.
  • Check whether an optional feature flag property exists on a config object before reading it.
  • Use bracket notation to access a property whose name is stored in a variable at runtime.

More examples

Dot vs bracket notation

Contrasts dot notation for static property names with bracket notation for dynamic key lookups.

Example · js
const car = { make: "Toyota", model: "Camry", year: 2023 };
const key = "model";
console.log(car.make);    // "Toyota"
console.log(car[key]);   // "Camry" – dynamic key

hasOwnProperty check

Uses Object.hasOwn to safely check whether a property exists directly on the object.

Example · js
const settings = { theme: "dark", lang: "en" };
console.log(Object.hasOwn(settings, "theme")); // true
console.log(Object.hasOwn(settings, "font"));  // false

Object.keys and Object.entries

Uses Object.entries to iterate over key-value pairs and destructure each entry in a forEach loop.

Example · js
const scores = { alice: 95, bob: 80, carol: 88 };
Object.entries(scores).forEach(([name, score]) => {
  console.log(`${name}: ${score}`);
});

Discussion

  • Be the first to comment on this lesson.