site logo

Ask. Code. Learn. Grow with the Developer Community.


Category: (All)
❮  Go Back

How to Remove a Property from a JavaScript Object

Sometimes in JavaScript, you need to remove a property from an object while keeping the rest of the object intact. This can be done using the delete operator or modern destructuring techniques. This guide explains both methods with examples, showing how to either mutate the original object or create a new object without certain properties.

How to Remove a Property from a JavaScript Object

coldshadow44 on 2025-10-13





Make a comment


Showing comments related to this post.

_

2025-10-14

Method 1: Using the delete Operator

The simplest way to remove a property from an object is with the delete keyword. This mutates the original object:

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

// Remove the 'regex' property
delete myObject.regex;
// or
delete myObject['regex'];

// Result
console.log(myObject);
// Output: { ircEvent: "PRIVMSG", method: "newURI" }

You can also use a variable to dynamically delete a property:

let prop = "regex";
delete myObject[prop];

Method 2: Using Object Destructuring (Non-Mutating)

If you want to create a new object without the property while leaving the original object intact, you can use destructuring with the rest operator:

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

// Destructure and exclude 'regex'
const { regex: _, ...newObj } = myObject;

console.log(newObj); // { ircEvent: "PRIVMSG", method: "newURI" }
console.log(myObject); // Original object remains unchanged

Key Points

  1. delete mutates the original object.
  2. Destructuring with rest creates a new object and preserves immutability.
  3. Both methods are widely supported in modern browsers and are standard practices in JavaScript development.





Member's Sites: