Deleting a property from an object
1. The delete operator
The simplest and most common way:
const user = {
name: 'Tim',
age: 25,
city: 'Kyiv'
};
delete user.age;
console.log(user);
// { name: 'Tim', city: 'Kyiv' }The delete operator removes a property directly from the object.
Important:
-
deleteonly works with object properties, not with variables. -
It returns
trueeven if the property did not exist:javascriptdelete user.nonexistent; // true -
It does not affect the prototype, only the object itself.
2. Via destructuring (an immutable approach)
If you want to create a new object without a property, without changing the original:
const user = { name: 'Tim', age: 25, city: 'Kyiv' };
const { age, ...updatedUser } = user;
console.log(updatedUser);
// { name: 'Tim', city: 'Kyiv' }Here ...rest collects all the remaining properties,
so the removal happens "softly", without changing the original.
3. Using Reflect.deleteProperty()
This is a modern alternative to delete, equivalent in meaning,
but used in more formal scenarios (for example, in metaprogramming).
const user = { name: 'Tim', age: 25 };
Reflect.deleteProperty(user, 'age');
console.log(user); // { name: 'Tim' }The behavior is the same as delete, but it returns true/false more strictly.
4. Nulling out or reassigning (when you do not need to physically remove it)
Sometimes it is safer not to delete, but just to clear the value:
user.age = null; // value logically removed
// or
user.age = undefined;Such a property stays in the object, but its value is considered "empty".
Summary
| Way | Changes the original object | What it does | Note |
|---|---|---|---|
delete obj.key | Yes | Removes the property | Simple and common way |
Reflect.deleteProperty(obj, key) | Yes | The same, but "officially" | Used in metaprogramming |
Destructuring { key, ...rest } | No | Creates a new object without the property | Immutable approach |
obj.key = null | Yes | "Clears" the value but does not remove the key | Handy when resetting state |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.