Object Properties
Add, change and remove object properties.
Syntax
obj.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 withObject.values().
Example
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.
const car = { make: "Toyota", model: "Camry", year: 2023 };
const key = "model";
console.log(car.make); // "Toyota"
console.log(car[key]); // "Camry" – dynamic keyhasOwnProperty check
Uses Object.hasOwn to safely check whether a property exists directly on the object.
const settings = { theme: "dark", lang: "en" };
console.log(Object.hasOwn(settings, "theme")); // true
console.log(Object.hasOwn(settings, "font")); // falseObject.keys and Object.entries
Uses Object.entries to iterate over key-value pairs and destructure each entry in a forEach loop.
const scores = { alice: 95, bob: 80, carol: 88 };
Object.entries(scores).forEach(([name, score]) => {
console.log(`${name}: ${score}`);
});
Discussion