<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alejandro Garcia Serna</title>
    <description>The latest articles on DEV Community by Alejandro Garcia Serna (@alejogs4).</description>
    <link>https://dev.to/alejogs4</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F151019%2F24b6e74b-8c90-4aeb-9dbe-53bf43e23263.jpeg</url>
      <title>DEV Community: Alejandro Garcia Serna</title>
      <link>https://dev.to/alejogs4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alejogs4"/>
    <language>en</language>
    <item>
      <title>Explaining loose-equality in javascript</title>
      <dc:creator>Alejandro Garcia Serna</dc:creator>
      <pubDate>Fri, 03 May 2019 04:09:50 +0000</pubDate>
      <link>https://dev.to/alejogs4/explaining-loose-equality-in-javascript-55cd</link>
      <guid>https://dev.to/alejogs4/explaining-loose-equality-in-javascript-55cd</guid>
      <description>&lt;p&gt;One of the most "strange" behaviors of javascript is how the &lt;code&gt;==&lt;/code&gt; equality operator works. You can see pieces of code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;42&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;On the internet, you'll find one common explanation about this, and is that &lt;code&gt;==&lt;/code&gt; operator compares only the value in both sides of the equality without looking the &lt;code&gt;type&lt;/code&gt;. This is a correct explanation but is not very accurate.&lt;/p&gt;

&lt;p&gt;One more accurate explanation is that &lt;code&gt;==&lt;/code&gt; reduces both sides of the equality to their &lt;code&gt;number representation&lt;/code&gt; and there, it makes the comparison. Consider the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Zero is already a number, but what happens with &lt;code&gt;false&lt;/code&gt;?&lt;br&gt;
If you convert &lt;code&gt;false&lt;/code&gt; to a number you'll get this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, the final comparison will be this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Having this in mind, check this out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The comparison above, follows the same rule that I've mentioned before. the empty array &lt;code&gt;[]&lt;/code&gt; if is converted to a number, gives as result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;On the other side of the comparison, we have &lt;code&gt;![]&lt;/code&gt; as we have the &lt;code&gt;!&lt;/code&gt; operator, this one changes the boolean value of the empty array. The boolean representation of &lt;code&gt;[]&lt;/code&gt; is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And as we have the &lt;code&gt;!&lt;/code&gt; operator the result is &lt;code&gt;false&lt;/code&gt;, and as we saw above, the number representation of &lt;code&gt;false&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As final comparison we are going to have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I really hope that this blog post help you to understand this behavior of javascript, the feedback is welcome and if you like this content I would be very happy if you share it. &lt;br&gt;
Thanks for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
