How to remove a property from JavaScript object ?
delete keyword is used to delete properties of an object in javaScript.
Syntax:
delete object.property or delete object[property]
Note:
- Delete keyword deletes the both value of the property and property also.After deletion, the property can not be used.
- Delete operator is designed to used on object properties. It can not be used on variables or functions.
- Delete operator should not be used on predefined JavaScript object properties. It can cause problems.
Example 1: This example deletes the address property of an object.
<!DOCTYPE html> <html> <head> <title></title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id="demo">the address of person1 is address1</p> <button onClick="fun()">click </button> <script> function fun(){ let p = { name: "person1", age:50, address:"address1" }; delete p.address; document.getElementById("demo").innerHTML = "the address of "+ p.name +" is " + p.address; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking the button:
-
After clicking the button:
-
Before clicking the button:
-
After clicking the button:
- Remove blank attributes from a JavaScript Object
- How to remove CSS property using JavaScript?
- JavaScript | Get the index of an object by its property.
- How to access an object having spaces in the object's key using JavaScript ?
- How to check a JavaScript Object is a DOM Object ?
- How to get the first key name of a JavaScript object ?
- How to get a key in a JavaScript object by its value ?
- Map vs Object in JavaScript
- Object.is( ) in JavaScript
- JavaScript | Object Constructors
- JavaScript | Math Object
- How to add an object to an array in JavaScript ?
- Object.isSealed( ) In JavaScript
- How to get the size of a JavaScript object?
- How to access first value of an object using JavaScript ?
-
Example 2: This example deletes the age property of an object.
<!DOCTYPE html> <html> <head> <title></title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id="demo">person1 is 50 years old.</p> <button onClick="fun()">click </button> <script> function fun(){ let p = { name: "person1", age:50, address:"address1" }; delete p.age; document.getElementById("demo").innerHTML = p.name + " is " + p.age + " years old. "; } </script> </body> </html> |
chevron_right
filter_none
Output:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



