{"id":854,"date":"2022-08-30T15:08:17","date_gmt":"2022-08-30T15:08:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/javascript-identify-whether-a-property-is-defined-and-set-to-undefined-or-undefined-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:08:17","modified_gmt":"2022-08-30T15:08:17","slug":"javascript-identify-whether-a-property-is-defined-and-set-to-undefined-or-undefined-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-identify-whether-a-property-is-defined-and-set-to-undefined-or-undefined-collection-of-common-programming-errors\/","title":{"rendered":"JavaScript &#8211; Identify whether a property is defined and set to &#39;undefined&#39;, or undefined-Collection of common programming errors"},"content":{"rendered":"<p>object.hasOwnProperty(name) only returns true for objects that are in the same object, and false for everything else, including properties in the prototype.<\/p>\n<pre><code>function x() {\n  this.another=undefined;\n};\n\nx.prototype.something=1;\nx.prototype.nothing=undefined;\ny = new x;\n\ny.hasOwnProperty(\"something\"); \/\/false\ny.hasOwnProperty(\"nothing\"); \/\/false\ny.hasOwnProperty(\"another\"); \/\/true\n\n\"someting\" in y; \/\/true\n\"another\" in y; \/\/true\n<\/code><\/pre>\n<p>Additionally the only way do delete a property is to use delete. Setting it to undefined do NOT delete it.<\/p>\n<p>The proper way to do it is to use <strong>in<\/strong> like roborg said.<\/p>\n<p>Update: <strong>undefined<\/strong> is a primitive value, see ECMAScript Language Specification section 4.3.2 and 4.3.9.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:49:05. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>object.hasOwnProperty(name) only returns true for objects that are in the same object, and false for everything else, including properties in the prototype. function x() { this.another=undefined; }; x.prototype.something=1; x.prototype.nothing=undefined; y = new x; y.hasOwnProperty(&#8220;something&#8221;); \/\/false y.hasOwnProperty(&#8220;nothing&#8221;); \/\/false y.hasOwnProperty(&#8220;another&#8221;); \/\/true &#8220;someting&#8221; in y; \/\/true &#8220;another&#8221; in y; \/\/true Additionally the only way do delete a property [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-854","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=854"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/854\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}