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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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


Output: