{"id":529,"date":"2022-08-30T15:02:52","date_gmt":"2022-08-30T15:02:52","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/null-and-undefined-in-javascript-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:52","modified_gmt":"2022-08-30T15:02:52","slug":"null-and-undefined-in-javascript-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/null-and-undefined-in-javascript-collection-of-common-programming-errors\/","title":{"rendered":"Null and undefined in javascript-Collection of common programming errors"},"content":{"rendered":"<p><code>undefined<\/code> is a variable on the global object which is <code>window<\/code> in browser environments. Its initial value is the primitive <code>undefined<\/code>.<\/p>\n<p>Being a property on the global object, historically you could have changed its value as,<\/p>\n<pre><code>window.undefined = \"42\"; \/\/ or just undefined = \"42\"\n<\/code><\/pre>\n<p>The meaning of life is now clearly defined. But since EcmaScript-5 is out, this has been disallowed, and even though it is still a property of the global object, it has been made non-writable now.<\/p>\n<p>The primitives <code>null<\/code> and <code>undefined<\/code> are not the same thing if no tampering has occurred.<\/p>\n<p><code>Null<\/code> is a data type that has the sole value <code>null<\/code>. <code>Undefined<\/code> is another data type whose sole value is the primitive <code>undefined<\/code>. You can verify whether they represent the same object or not easily.<\/p>\n<pre><code>null === undefined \/\/ false\n<\/code><\/pre>\n<p>However,<\/p>\n<pre><code>null == undefined \/\/ true\n<\/code><\/pre>\n<p>is true, because they are both casted to the boolean value <code>false<\/code> before a comparison is made. The rules for converting both these values to boolean are clearly defined in section 9.2 of the spec.<\/p>\n<pre>\n9.2 ToBoolean\n\nArgument Type | Result\n-------------------------------------------------------------------------\nUndefined     | false\nNull          | false\nBoolean       | The result equals the input argument (no conversion).\nNumber        | ..\nString        | ..\nObject        | ..\n<\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:19:20. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>undefined is a variable on the global object which is window in browser environments. Its initial value is the primitive undefined. Being a property on the global object, historically you could have changed its value as, window.undefined = &#8220;42&#8221;; \/\/ or just undefined = &#8220;42&#8221; The meaning of life is now clearly defined. But since [&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-529","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/529","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=529"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/529\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}