{"id":954,"date":"2022-08-30T15:09:57","date_gmt":"2022-08-30T15:09:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/null-is-type-object-so-its-truthy-whats-going-on-behind-the-scenes-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:57","modified_gmt":"2022-08-30T15:09:57","slug":"null-is-type-object-so-its-truthy-whats-going-on-behind-the-scenes-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/null-is-type-object-so-its-truthy-whats-going-on-behind-the-scenes-collection-of-common-programming-errors\/","title":{"rendered":"Null is type object, so it&#39;s truthy? What&#39;s going on behind the scenes?-Collection of common programming errors"},"content":{"rendered":"<p>When you compare <code>null<\/code> to <code>true<\/code>, <strike>it&#8217;s evaluated as <code>false<\/code>,<\/strike> so it&#8217;s not equal to <code>true<\/code>. Similarly, when used in any other context where it must be treated as a boolean \u2014 like an <code>if<\/code> or <code>while<\/code> expression \u2014 it&#8217;s <code>false<\/code>.<\/p>\n<p>It&#8217;s not really correct to say that &#8220;null is type object&#8221;, because it is not. It is <code>null<\/code>. <strike>It doesn&#8217;t really have any type,<\/strike> (Thanks @Roee Gavirel) It&#8217;s got it&#8217;s own type (the null type) because it isn&#8217;t anything. In other words, if a variable has the value <code>null<\/code>, that means that it is referring to nothing; no object at all.<\/p>\n<p><em>edit<\/em> \u2014 crap hold on a sec because my brain&#8217;s still asleep.<\/p>\n<p>OK here&#8217;s what&#8217;s in the spec. If one of the operands of <code>==<\/code> is boolean, then the boolean is converted to a number (yes really) and the conversion proceeds that way. That&#8217;s why <code>null<\/code> is <code>==<\/code> to neither <code>true<\/code> nor <code>false<\/code>. The &#8220;strangeness&#8221;, therefore, is not so much about <code>null<\/code> as it is about the complicated rules for evaluating <code>==<\/code> comparisons.<\/p>\n<p>Section 11.9.3 of the ECMA-262 standard spells it all out in a more-or-less understandable way. Suffice to say that the semantics of <code>==<\/code> are not at all simple.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:06:05. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>When you compare null to true, it&#8217;s evaluated as false, so it&#8217;s not equal to true. Similarly, when used in any other context where it must be treated as a boolean \u2014 like an if or while expression \u2014 it&#8217;s false. It&#8217;s not really correct to say that &#8220;null is type object&#8221;, because it is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-954","post","type-post","status-publish","format-standard","hentry","category-semantic","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/954","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=954"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/954\/revisions"}],"predecessor-version":[{"id":8812,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/954\/revisions\/8812"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}