How Do I Remove A Property From A JavaScript Object?

Asked 2 years ago
Answer 1
Viewed 582
1

Objects in JavaScript can be thought of as mappings between keys and values. The remove operator is used to individually remove these keys, better known as of object properties.

var obj = {
  myProperty: 1    
}
console.log(obj.hasOwnProperty('myProperty')) // true
delete obj.myProperty
console.log(obj.hasOwnProperty('myProperty')) // false

The delete operator does not free memory directly and differs from simply assigning a null or undefined value to a property in that the property itself is deleted from the object. Note that if the value of a deleted property was a reference type (an object) and another part of your program still has a reference to that object, that object will of course not be included in the garbage collector until until all references to it have been collected. gone.

Deleting only works on properties whose descriptor marks them as configurable.

Don't miss it : Top 10 tricky JavaScript questions that I used to ask in interviews

Answered one year ago White Clover   MarketsWhite Clover Markets