{"id":1732,"date":"2017-04-22T00:53:07","date_gmt":"2017-04-22T00:53:07","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1732"},"modified":"2017-05-22T23:28:25","modified_gmt":"2017-05-22T23:28:25","slug":"understanding-delete-operator-in-javascript","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2017\/04\/understanding-delete-operator-in-javascript\/","title":{"rendered":"Understanding delete operator in Javascript"},"content":{"rendered":"<p>Delete operator is used to delete the property of an object. It completely remove the property and keep the rest of the object intact.<\/p>\n<ul>\n<li>The delete operator will not delete ordinary variables.\n<\/li>\n<li>It cannot delete properties of the global object, which were declared with the var keyword.\n<\/li>\n<li>However, it will delete &#8220;global variables,\u201d which were not declared with var keyword. since  they are actually properties of the global object ( window in the browser).\n<\/li>\n<li>The delete operator doesn&#8217;t delete prototype property.\n<\/li>\n<\/ul>\n<p>For examples:<\/p>\n<pre>\r\nvar person = { rank : 1};\r\nvar result = (function(){\r\n    delete person.rank;\r\n    return person.rank;\r\n  })();\r\n  console.log(result); \/\/ undefined\r\n<\/pre>\n<p> The result would be undefined. In the above code, we have an object named as \u201cperson\u201d which has the property \u201crank\u201d, and as it is a self-invoking function, we will delete the \u201crank\u201d property from object \u201cperson\u201d.  When we console.log the reference of the property the result will be undefined. <\/p>\n<p class=\"phpmind_text_heading_green\">Let us go through some examples where delete operator doesn\u2019t work:\n<\/p>\n<pre>var result = (function(number){\r\n    delete number;\r\n    return number;\r\n  })(5);  \r\n  console.log(result); \/\/ 5\r\n<\/pre>\n<p>The result would be 5. Delete operators don&#8217;t affect local variables. Here, \u201cnumber\u201d is not an object but a local variable. The delete operator will not delete ordinary variables. <\/p>\n<pre> var number = 5;\r\nvar result = (function(){\r\n    delete number;\r\n    return number;\r\n  })();\r\n  console.log(result); \/\/ 5\r\n<\/pre>\n<p>The result would be 5. Delete operators don&#8217;t affect global variables declared with var keyword. Here, \u201cnumber\u201d is not an object but a global variable of type number. <\/p>\n<pre> number = 5; \/\/ var is not used. hence it is a property of window.\r\ndelete window.number;\r\nconsole.log(number); \/\/ ReferenceError: number is not defined\r\n<\/pre>\n<p>The result would be \u201cnumber is not defined\u201d. Here, the global variable \u201cnumber\u201d is not defined with var keyword. hence, the delete operator will delete &#8220;number&#8221; since they are actually properties of the global object (window in the browser).<\/p>\n<pre>\r\nvar team = {\r\n  name: 'Jane'\r\n};\r\nvar team2 = Object.create(team);\r\ndelete team2.name;\r\nconsole.log(team2.name); \/\/ Jane\r\n<\/pre>\n<p>The result would be \u201cJane\u201d. Delete operator will not affect prototype property. Here, \u201cname\u201d is a prototype property of team2.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Delete operator is used to delete the property of an object. It completely remove the property and keep the rest of the object intact. The delete operator will not delete ordinary variables. It cannot delete properties of the global object, which were declared with the var keyword. However, it will delete &#8220;global variables,\u201d which were [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[27],"tags":[],"class_list":["post-1732","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1732"}],"version-history":[{"count":6,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1732\/revisions"}],"predecessor-version":[{"id":1740,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1732\/revisions\/1740"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}