Web

JavaScript Delete: Remove Property from Object

Learn javascript delete operator, Reflect.deleteProperty, object rest/spread to remove properties. Examples for js delete object, js delete object property, js object delete key without mutating.

1 answer 4 views

How do I remove a property from a JavaScript object?

Given an object:

javascript
let myObject = {
 "ircEvent": "PRIVMSG",
 "method": "newURI",
 "regex": "^http://.*"
};

How do I remove the property regex to end up with the following myObject?

javascript
let myObject = {
 "ircEvent": "PRIVMSG",
 "method": "newURI"
};

What are the different methods to delete a property from a JavaScript object, and what are the differences between them?

javascript delete (js delete object): the simplest way is the delete operator — for your example run delete myObject.regex; (or delete myObject['regex']) to remove the key in place. If you don’t want to mutate the original object, use object rest/spread, Object.assign/Object.fromEntries, or create a filtered copy; Reflect.deleteProperty(obj, 'regex') is a boolean-returning alternative to delete.


Contents


Quick example: remove a property from an object

Given your object:

javascript
let myObject = {
 ircEvent: "PRIVMSG",
 method: "newURI",
 regex: "^http://.*"
};

Quickest, in-place removal (mutates the object):

javascript
delete myObject.regex; // or: delete myObject['regex'];
console.log(myObject);
// { ircEvent: "PRIVMSG", method: "newURI" }

That delete call removes the regex own property from myObject. If you need a non‑mutating result, see the rest/spread and copy examples below.


Use the delete operator (javascript delete)

What it does

  • delete obj.prop removes an own property from an object (both dot and bracket notation work).
  • For properties created by an object literal (like your regex), delete will normally succeed and return true.

Syntax examples:

javascript
delete myObject.regex;
delete myObject['regex'];

const key = 'regex';
delete myObject[key];

Return value and strict mode

  • In non-strict code delete returns true if the property was deleted or if the property didn’t exist; it returns false if the property exists but is non-configurable.
  • In strict mode, attempting to delete a non-configurable property will throw a TypeError. See the MDN page on the delete operator for details.

Own vs prototype

  • delete only affects own properties. If a property comes from the prototype chain, delete obj.prop won’t remove it from the prototype (delete would have to be used on the prototype object itself).

Check existence:

  • After deleting, myObject.hasOwnProperty('regex') is false, and 'regex' in myObject is false (unless the prototype defines it).

Use Reflect.deleteProperty (js delete object)

Reflect provides a function form:

javascript
const ok = Reflect.deleteProperty(myObject, 'regex'); // true/false

Differences vs delete

  • Reflect.deleteProperty always returns a boolean reflecting the success of the operation; it won’t throw just because code is running in strict mode. That can be handy when you need a predictable result value from a function call.
  • Semantically it follows the same internal [[Delete]] semantics (so non-configurable properties still block deletion).

See the MDN entry for Reflect.deleteProperty.


Create a new object without the property (js delete object property)

If you prefer not to mutate the original object (common in functional code, React/Redux, etc.), make a copy that omits the key.

Object rest (ES2018+):

javascript
const { regex, ...withoutRegex } = myObject;
console.log(withoutRegex);
// { ircEvent: "PRIVMSG", method: "newURI" }

One-liner (useful for mapping/filtering):

javascript
const newObj = Object.fromEntries(
 Object.entries(myObject).filter(([k]) => k !== 'regex')
);

Or copy + delete:

javascript
const copy = Object.assign({}, myObject);
delete copy.regex;

Notes

  • Rest/spread and Object.fromEntries produce new objects; original myObject is unchanged unless you reassign it (myObject = withoutRegex if myObject is let).
  • Object rest/spread details: see MDN on object rest in destructuring and Object.fromEntries.

Setting to undefined vs deleting (js object delete key)

Setting a property to undefined is not the same as deleting it:

javascript
myObject.regex = undefined;

console.log(myObject.hasOwnProperty('regex')); // true
console.log('regex' in myObject); // true
console.log(JSON.stringify(myObject)); // regex may be omitted in JSON output

Key differences

  • delete removes the key entirely; undefined keeps the key with the value undefined.
  • Object.keys() and for...in will still list a property set to undefined.
  • JSON.stringify typically omits object properties with undefined values (so for serialization there can be similar effects, but the runtime object shape is different).

If you want the property gone, use delete or create a filtered copy.


Arrays, prototype and performance caveats

Arrays

  • delete arr[index] removes the element but leaves a hole (sparse array). The length is unchanged. Prefer arr.splice(index, 1) to remove an element and shift indexes.

Prototype properties

  • If a property is inherited from the prototype, deleting it from an instance won’t remove the prototype property. To remove it you must delete it from the prototype object itself (rarely desired).

Non-configurable properties

  • Properties with configurable: false (created via Object.defineProperty) can’t be deleted. Deletion will fail (return false) or throw in strict mode.

Performance

  • Repeatedly adding/removing properties can change an object’s internal “shape” and may deoptimize JIT optimizations in some JS engines. If you need to remove lots of keys frequently, consider creating new objects (copy-on-write) or designing data structures to avoid churn.

When to mutate vs copy?

  • Mutating with delete is fine for one-off changes or small scripts.
  • For predictable state updates (UI state, Redux, pure functions) prefer non-mutating copies.

Sources


Conclusion

For a quick in-place removal use delete myObject.regex (javascript delete); if you want a boolean-returning function use Reflect.deleteProperty(myObject, 'regex'). Prefer a non‑mutating copy (object rest/spread, Object.fromEntries, or Object.assign + delete) when you need immutable updates. Set a property to undefined only when you intend the key to remain present — otherwise actual deletion is the right choice.

Authors
Verified by moderation
Moderation
JavaScript Delete: Remove Property from Object