How to Add and Remove a Property of an Object in Javascript

Saturday, April 17, 2021 • One minute to read

Javascript2021to-the-pointjavascriptto-the-point2021

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

1const obj = {};
2// syntax 1
3obj.property1 = "first property"
4// or syntax 2
5obj.property2 = "second property"

Remove or delete a property of an Object

Use delete keyword to remove or delete a property.

 1const obj = {
 2    "property1": "first property",
 3    "property2": "second property",
 4    "property3": "third property"
 5};
 6
 7// delete second property
 8delete obj.property2;
 9// delete third property
10delete obj["property3"];

Javascript2021to-the-pointjavascriptto-the-point2021

PreviousHow to Check if Object Is Empty in Javascript

NextHow to Convert an Array to a String in Javascript