JavaScript delete Operator (With Example)

JavaScript delete Operator: In this tutorial, we will learn about the delete operator in JavaScript with its syntax, return type, and examples. By IncludeHelp Last updated : July 29, 2023

JavaScript delete Operator

In JavaScript, the delete operator is used to delete a property of an object. This operator accepts a property. After deleting the property if you try to access it, undefined will return.

Syntax

Below is the syntax of JavaScript delete operator:

delete object.property
delete object[property]

Parameter

  • object.property/object[property] - This operator accepts the property of an object to be deleted.

Return Value

The delete operator returns "true", it returns "false" when the property is an own non-configurable.

Example

This example demonstrates the use of JavaScript delete property.

<html>
  <head>
    <title>JavaScipt Example</title>
  </head>
  <body>
    <script>		
      var employee = {
      	name: "Amit shukla",
      	age: 21,
      	city: "Gwalior",
      	Country: "India"
      };
      
      //printing the object
      document.write("Printing employee details...<br>");
      document.write("Name: " + employee.name + "<br/>");
      document.write("Age: " + employee.age + "<br/>");
      document.write("City: " + employee.city + "<br/>");
      document.write("Country: " + employee.Country + "<br/>");
      document.write("<br>");
      
      //deleting properties age & city
      delete employee.age;
      delete employee.city;
      
      document.write("Printing employee details after delete...<br>");
      document.write("Name: " + employee.name + "<br/>");
      document.write("Age: " + employee.age + "<br/>");
      document.write("City: " + employee.city + "<br/>");
      document.write("Country: " + employee.Country + "<br/>");		
    </script>
  </body>
</html>

Output

The output of the above example is:

Printing employee details...
Name: Amit shukla
Age: 21
City: Gwalior
Country: India

Printing employee details after delete...
Name: Amit shukla
Age: undefined
City: undefined
Country: India

JavaScript Tutorial »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.