Skip to content
Related Articles
Open in App
Not now

Related Articles

How to remove a property from JavaScript object ?

Improve Article
Save Article
  • Last Updated : 03 Jan, 2023
Improve Article
Save Article

JavaScript 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 the property also. After deletion, the property can not be used.
  • The delete operator is designed to use on object properties. It can not be used on variables or functions.
  • Delete operators should not be used on predefined JavaScript object properties. It can cause problems.

Example 1: This example deletes the address property of an object. 

html




<h1 style="color:green;">
    GeeksforGeeks
</h1>
<p id="demo">the address of person1 is </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>


Output:

remove a property from JavaScript object

Remove a property from JavaScript object

Example 2: This example deletes the age property of an object.

HTML




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


Output:

Remove a property from JavaScript object

Remove a property from JavaScript object


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!