Skip to content
Related Articles

Related Articles

How to remove a property from JavaScript object ?
  • Last Updated : 27 Mar, 2019

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>

Output:

  • Before clicking the button:
  • After clicking the button:
      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>

      Output:

      • Before clicking the button:
      • After clicking the button:



          My Personal Notes arrow_drop_up
Recommended Articles
Page :