How to Add and Remove a Property of an Object in Javascript
Javascript object is a collection of properties and a property is a key-value pair.
The value can be anything like any data type, object and even a function. When a value is a function then it is known as method.
Add a property to an object
const obj = {};
// syntax 1
obj.property1 = "first property"
// or syntax 2
obj.property2 = "second property"
Remove or delete a property of an Object
Use delete
keyword to remove or delete a property.
const obj = {
"property1": "first property",
"property2": "second property",
"property3": "third property"
};
// delete second property
delete obj.property2;
// delete third property
delete obj["property3"];