How to delete a property of an object in JavaScript?


To delete a property of an object, delete key word should be used. Delete key word can be used with both the methods such as Dot method and Bracket method.

syntax

delete object.property;

Example

In the following example initially when the property "country" is executed its value "England" is displayed in the output. But when that property is deleted using delete keyword,instead of "England", undefined is displayed as shown in the output.

Live Demo

<html>
<body>
<script>
   var txt = "";
   var person = {
      "name":"Ram",
      "age":27,
      "address": {
         "houseno" : 123,
         "streetname" : "Baker street",
         "country": "England"
      }
   }
   document.write("Before deletion :" + " "+ person.address.country);
   delete person.address.country;
   document.write("</br>");
   document.write("After deletion :" + " "+ person.address.country);
</script>
</body>
</html>

Output

Before deletion : England
After deletion : undefined

Updated on: 29-Jun-2020

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements