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:
- How to check a JavaScript Object is a DOM Object ?
- How to access an object having spaces in the object's key using JavaScript ?
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- Remove blank attributes from a JavaScript Object
- How to remove object from array of objects using JavaScript ?
- JavaScript | Get the index of an object by its property.
- How to get all property values of a JavaScript Object (without knowing the keys) ?
- Access Object property case-insensitively in JavaScript
- JavaScript TypeError - Can't assign to property "X" on "Y": not an object
- How to add a property to a JavaScript object using a variable as the name?
- JavaScript Object.prototype.constructor Property
- How to remove CSS property using JavaScript?
- PHP | Type Casting and Conversion of an Object to an Object of other class
- HTML | DOM Object Object
- How to select first object in object in AngularJS?
- JavaScript | Adding minutes to Date object
- Object.seal( ) In JavaScript
- Object.is( ) in JavaScript
- Object.assign( ) in JavaScript
- Object.freeze( ) in 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.


