{"id":1317,"date":"2014-08-05T23:41:11","date_gmt":"2014-08-06T04:41:11","guid":{"rendered":"http:\/\/cbateman.com\/blog\/?p=1317"},"modified":"2014-10-09T22:30:51","modified_gmt":"2014-10-10T03:30:51","slug":"how-double-equals-works-in-javascript","status":"publish","type":"post","link":"https:\/\/cbateman.com\/blog\/how-double-equals-works-in-javascript\/","title":{"rendered":"How Double-Equals Works in JavaScript"},"content":{"rendered":"<p><strong>tl;dr<\/strong> &mdash; Don&#8217;t use double-equals!<\/p> \r\n\r\n<p>When asked about the difference between double and triple-equals, a lot of JS developers will tell you that <code>==<\/code> compares values and <code>===<\/code> compares values <em>and<\/em> types. In other words, <code>1 == \"1\"<\/code> will be true, and <code>1 === \"1\"<\/code> will be false.<\/p>\r\n\r\n<p>That explanation is somewhat true, but it belies a much more complicated reality.<\/p>\r\n\r\n<p>Some of the confusion comes from thinking that <code>==<\/code> is somehow related to truthiness, which is all about how variables get coerced into booleans &mdash; which happens in an <i>if statement<\/i> like this:<\/p>\r\n\r\n<pre class=\"pre\">if (something) {<\/pre>\r\n\r\n<p>In that case &mdash; <code>0<\/code>, <code>\"\"<\/code> the empty string, <code>null<\/code>, <code>undefined<\/code>, and <code>NaN<\/code> will all return false. Non-zero numbers, Non-empty strings, arrays and objects (empty or not) will all return true. Most devs have a pretty good handle on this.<\/p>\r\n\r\n<p>But&#8230; does that mean that a non-zero number, or all non-empty strings will double-equal true? This is where things can get confusing.<\/p>\r\n\r\n<pre class=\"pre\">if ('a') {           \/\/ true\r\nif ('a' == true) {   \/\/ false\r\nif ('a' == false) {  \/\/ false\r\n\r\nif (2) {             \/\/ true\r\nif (2 == true) {     \/\/ false\r\nif (1 == true) {     \/\/ true\r\n<\/pre>\r\n\r\n<p>Hopefully that makes it clear that truthiness has nothing to do with <code>==<\/code>.<\/p>\r\n\r\n<p>Remember: <strong>truthiness is about coercion into booleans. With double-equals, nothing will ever get coerced into a boolean.<\/strong> So what&#8217;s really going on?<\/p>\r\n\r\n<p>The answer is the Abstract Equality Comparison Algorithm. If the two types differ, JS follows a particular process for converting them into the same type, so that it can compare them. If types don&#8217;t match somewhere along the way &mdash; the endgame will be numbers.<\/p>\r\n\r\n<p>\r\n  <ul>\r\n  <li>First, booleans are converted to numbers. <code>True<\/code> becomes <code>1<\/code> and <code>false<\/code> becomes <code>0<\/code>.<\/li>\r\n  <li>Next, objects will be turned into strings using <code>.toString()<\/code> (unless you modified <code>.valueOf()<\/code> to return a primitive). So <code>[1]<\/code> becomes <code>\"1\"<\/code>, <code>[1,2]<\/code> becomes <code>\"1,2\"<\/code>, and both <code>{...}<\/code> and <code>[{...}]<\/code> become <code>\"[object Object]\"<\/code>.<\/li>\r\n  <li>If a string and a number are left, the string is converted to a number (so any string with non-number characters will become <code>NaN<\/code> &mdash; which, by the way, never ever equals anything, including itself).<\/li>\r\n  <li><code>null<\/code> and <code>undefined<\/code> equal themselves and each other.<\/li>\r\n  <\/ul>\r\n<\/p>\r\n\r\n<p>That&#8217;s the gist of it, but you can check out <a href=\"http:\/\/www.ecma-international.org\/ecma-262\/5.1\/#sec-11.9.3\">the spec<\/a> for more details.<\/p>\r\n\r\n<p>So &mdash; do you need to remember <a href=\"http:\/\/dorey.github.io\/JavaScript-Equality-Table\/\">all these rules<\/a>? Absolutely not. As Felix Geisend\u00f6rfer <a href=\"https:\/\/github.com\/felixge\/node-style-guide#use-the--operator\">puts it<\/a>, &#8220;Programming is not about remembering stupid rules. Use the triple equality operator as it will work just as expected.&#8221;<\/p>\r\n\r\n<p>That&#8217;s why most all JS style guides recommend using only <code>===<\/code>. <a href=\"http:\/\/contribute.jquery.org\/style-guide\/js\/#equality\">Some<\/a> allow for an exception when checking for null or undefined (by using &#8220;<code>== null<\/code>&#8220;). But some folks would <a href=\"http:\/\/www.2ality.com\/2011\/12\/strict-equality-exemptions.html\">argue against even that<\/a>.","protected":false},"excerpt":{"rendered":"<p>tl;dr &mdash; Don&#8217;t use double-equals! When asked about the difference between double and triple-equals, a lot of JS developers will tell you that == compares values and === compares values and types. In other words, 1 == &#8220;1&#8221; will be true, and 1 === &#8220;1&#8221; will be false. That explanation is somewhat true, but it belies a much more complicated&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1317","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/posts\/1317"}],"collection":[{"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/comments?post=1317"}],"version-history":[{"count":43,"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/posts\/1317\/revisions"}],"predecessor-version":[{"id":1368,"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/posts\/1317\/revisions\/1368"}],"wp:attachment":[{"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/media?parent=1317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/categories?post=1317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbateman.com\/blog\/wp-json\/wp\/v2\/tags?post=1317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}