In JavaScript, you can remove a property from an object using the delete operator. Here's how you can do it:
var obj = { property1: "value1", property2: "value2", }; // Remove a property delete obj.property1; // Check if the property is removed console.log(obj.property1); // Output: undefined
In the above example, we have an object obj
with two properties property1
and property2.
To remove property1
, we use the delete
operator followed by the object name and the property name (delete obj.property1)
.
After removing the property, if you try to access it, it will return undefined
, as demonstrated by console.log(obj.property1)
.
Note that the delete
the operator removes the property from the object directly. If the property is inherited from a prototype chain, it will not affect the original prototype object.
To know with better understanding, you can join the Java training in Indore, Delhi, Meerut, Noida, Lucknow, Jaipur, Bhopal, Mumbai and other cities in India, choose your nearby location which is acceptable to you.