Add, change and delete object properties
Quick answer
Assignment can add or update an ordinary object property. The delete operator attempts to remove an own property rather than assigning undefined.
How it works
Use record.count = 3 and delete record.count. On ordinary configurable fields delete returns true. Nonconfigurable properties cannot be deleted; strict mode throws instead of returning false. Object.hasOwn(record, key) checks own-property presence even when the value is undefined.
const item = { count: 2 };item.count = 5;item.active = true;console.log(item.count, item.active);Expected output in Node.js:
5 trueThe original object receives an updated and a new field.
const item = { count: undefined };console.log(Object.hasOwn(item, "count"));delete item.count;console.log(Object.hasOwn(item, "count"));Expected output in Node.js:
truefalseUndefined is a stored value; deletion removes the property.
Common mistakes
Do not use truthiness to test property existence. A field with zero can still exist. The in operator also considers inherited properties.
Practice
Create a record, set a field to undefined and then delete it; compare own-property checks.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Add, change and delete object properties — Knowledge Check
Apply the taught concept and reason about the result.
What does assigning item.count=4 on an ordinary writable object do?
What does delete item.count attempt to do?
What does Object.hasOwn({x:undefined},"x") return?
What does "toString" in {} typically return with the ordinary Object prototype?
What happens when deleting a nonconfigurable property in strict mode?