Removing a property from an object
A property is removed with the delete operator, and when the original object must stay untouched you use destructuring with rest syntax, which returns a new object without that key. On top of that there is Reflect.deleteProperty() for metaprogramming and plain nulling of the value when the key itself has to remain in the object.
Theory
TL;DR
delete obj.keyis the simplest and most common way, and it mutates the object.deleteworks only on object properties, not on variables.deletereturnstrueeven when the property did not exist, and it does not affect the prototype.const { key, ...rest } = objis the immutable way: it creates a new object without the key.Reflect.deleteProperty(obj, key)has the same effect but in plain function form.obj.key = nullorundefineddoes not remove the key, it only clears the value.
Quick example
const user = { name: 'Maria', age: 25, city: 'Kyiv' };
delete user.age;
console.log(user);
// { name: 'Maria', city: 'Kyiv' }The delete operator
The simplest and most common way:
const user = {
name: 'Maria',
age: 25,
city: 'Kyiv'
};
delete user.age;
console.log(user);
// { name: 'Maria', city: 'Kyiv' }The delete operator removes the property from the object directly.
Important details:
deleteworks only on object properties, not on variables.- It returns
trueeven when the property did not exist:
delete user.nonexistent; // true- It does not affect the prototype, only the object itself. If a property with the same name exists on the prototype, reading it after the own property is deleted starts returning the prototype one.
- In strict mode, deleting a non-configurable (
configurable: false) property throws aTypeError; outside strict mode it simply returnsfalse.
Destructuring (the immutable way)
When you want to create a new object without a property and leave the original alone:
const user = { name: 'Maria', age: 25, city: 'Kyiv' };
const { age, ...updatedUser } = user;
console.log(updatedUser);
// { name: 'Maria', city: 'Kyiv' }Here ...rest collects every remaining property, so the removal happens "softly", without changing the original. This is exactly the approach React and Redux need, where state is compared by reference. If the key name is only known at runtime, use a computed key: const { [key]: removed, ...rest } = user;.
Reflect.deleteProperty()
This is the modern alternative to delete, equivalent in meaning but handy in more formal scenarios such as metaprogramming:
const user = { name: 'Maria', age: 25 };
Reflect.deleteProperty(user, 'age');
console.log(user); // { name: 'Maria' }The behaviour matches delete, but it is an ordinary function, so the key can be passed in a variable and the result (true or false) is returned strictly, without throwing in strict mode.
Nulling or overwriting the value
Sometimes it is safer not to remove the property but simply to reset its value:
user.age = null; // the value is cleared logically
// or
user.age = undefined;Such a property stays in the object, but its value counts as "empty". The difference shows up immediately: 'age' in user still returns true and Object.keys(user) still lists the key. Note that JSON.stringify() drops properties whose value is undefined but keeps those equal to null.
Comparison of the ways
| Way | Mutates the original object | What it does | Note |
|---|---|---|---|
delete obj.key | Yes | Removes the property | Simple and common |
Reflect.deleteProperty(obj, key) | Yes | The same, but in function form | Used in metaprogramming |
Destructuring { key, ...rest } | No | Creates a new object without the property | The immutable approach |
obj.key = null | Yes | "Clears" the value but keeps the key | Handy when resetting state |
Common mistakes
- Believing
= undefineddeletes a property. The key stays:'age' in userreturnstrueandObject.keys()still shows it. Onlydeleteremoves it. - Relying on the result of
delete. It returnstruefor a non-existent property too, so it is not a success check. Test for the key withObject.hasOwn(). - Using
deleteon array elements.delete arr[1]leaves an empty slot and does not shrinklength. For arrays usesplice()orfilter(). - Expecting
deleteto remove an inherited property. It only removes an own property; anything coming from the prototype stays reachable. - Mutating a shared object. If the object is used in several places,
deletechanges it for all of them. When a new reference is needed, use rest destructuring.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.