How do you add and delete properties on a JavaScript object?
Assigning to a name that doesn't exist yet adds a new property; the delete keyword removes one.
JavaScript objects are flexible — their shape isn't fixed when you create them. To add a property, just assign to a name the object doesn't have yet; JavaScript creates it on the spot. To remove one, use the delete keyword:
let person = { firstName: 'Hans', name: 'Meier', age: 29 };
person.location = 'Rotkreuz'; // add: 'location' didn't exist, so it's created
delete person.age; // remove the 'age' property entirely
console.log(person);
// { firstName: 'Hans', name: 'Meier', location: 'Rotkreuz' }
Notice this is the flip side of reading: accessing a property that doesn't exist gives undefined, while assigning to one that doesn't exist creates it.
A maintainability note: while adding and deleting properties on the fly is allowed, it can make code harder to follow, because the object's shape changes at runtime and readers can't tell what fields it has by glancing at its definition. When you can, define the object's full structure up front and avoid reshaping it later.