Remove Property from Object in Javascript

Home /

Table of Contents

JavaScript Property

In JavaScript, a property is a characteristic or attribute of an object. It is a key-value pair, where the key (also known as the property name) is a string and the value can be of any data type (such as a number, string, or another object). Properties can be accessed and modified using the dot notation (e.g. object.property) or the bracket notation (e.g. object[‘property’]).

JavaScript Object

A JavaScript object is a collection of properties, which are represented as key-value pairs. Objects are used to store and manipulate data, and can be used to model real-world objects or abstract concepts. Objects can be created using object literals, which are enclosed in curly braces ({}), or by using the object constructor or the object initializer syntax. Once created, properties can be added, modified, or deleted from an object, and the values of the properties can be accessed using the dot notation (e.g. object.property) or the bracket notation (e.g. object[‘property’]). Objects can also have methods, which are functions that are properties of an object. Objects can also be created using class constructor.

Remove regex property from JavaScript Object

In JavaScript, you can remove a property from an object using the ‘delete’ operator. For example, if you have an object ‘obj’ and you want to remove a property ‘prop’, you can use the following code:

JavaScript
delete obj.prop;

Alternatively, you can also use the ‘Object.defineProperty()’ method with the ‘configurable’ property set to ‘false’, but this method is not widely supported.

JavaScript
Object.defineProperty(obj, "prop", {
  configurable: false
});

You also can use a ‘Object.assign’ or ‘spread operator’ (ES6) to create a new object without the property you want to delete

JavaScript
const { prop, ...rest } = obj;

This will remove the ‘prop’ property from the object and rest will have the remaining properties.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article