How to delete a property of an object in JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

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
raja
Published on 27-Jun-2019 19:03:48
Advertisements