Skip to main content

Deleting a property from an object

1. The delete operator

The simplest and most common way:

javascript
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:

  • delete only works with object properties, not with variables.

  • It returns true even if the property did not exist:

    javascript
    delete 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:

javascript
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).

javascript
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:

javascript
user.age = null; // value logically removed // or user.age = undefined;

Such a property stays in the object, but its value is considered "empty".


Summary

WayChanges the original objectWhat it doesNote
delete obj.keyYesRemoves the propertySimple and common way
Reflect.deleteProperty(obj, key)YesThe same, but "officially"Used in metaprogramming
Destructuring { key, ...rest }NoCreates a new object without the propertyImmutable approach
obj.key = nullYes"Clears" the value but does not remove the keyHandy when resetting state

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.