How to work with delete operator in JavaScript?

HTMLProgramming ScriptsJavascript

Use the delete property in JavaScript, to remove a property from an object. You can try to run the following code to learn how to work with delete operator. Here, we are deleting the book price −

Example

Live Demo
<html>
   <head>
      <title>JavaScript Delete Operator</title>
      <script>
         function book(title, author) {
            this.title = title;
            this.author = author;
         }
      </script>
   </head>
   
   <body>
      <script>
         var myBook = new book("WordPress Development", "Amit");
         book.prototype.price = null;
         myBook.price = 500;

         document.write("<h2>Details before deleting book price</h2>");
         document.write("Book title is : " + myBook.title + "<br>");
         document.write("Book author is : " + myBook.author + "<br>");
         document.write("Book price is : " + myBook.price);

         delete myBook.price;

         document.write("<br><h2>Details after deleting book price</h2>");
         document.write("Book title is : " + myBook.title + "<br>");
         document.write("Book author is : " + myBook.author + "<br>");
         document.write("Book price is : " + myBook.price + "<br>");
      </script>
      
   </body>
</html>
raja
Published on 26-Feb-2018 06:32:13
Advertisements