<?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: Emine Bozdag</title>
    <description>The latest articles on DEV Community by Emine Bozdag (@eminebozdag).</description>
    <link>https://dev.to/eminebozdag</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%2F991076%2F4ec670a6-035f-46e4-96bc-d187994eeab4.jpeg</url>
      <title>DEV Community: Emine Bozdag</title>
      <link>https://dev.to/eminebozdag</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eminebozdag"/>
    <language>en</language>
    <item>
      <title>Equality of Values in Javascript</title>
      <dc:creator>Emine Bozdag</dc:creator>
      <pubDate>Thu, 15 Dec 2022 20:41:49 +0000</pubDate>
      <link>https://dev.to/eminebozdag/equality-of-values-in-javascript-294c</link>
      <guid>https://dev.to/eminebozdag/equality-of-values-in-javascript-294c</guid>
      <description>&lt;p&gt;JavaScript has several built-in data types that are used to represent different kinds of values. The primitive data types in JavaScript include &lt;strong&gt;Number&lt;/strong&gt;, &lt;strong&gt;String&lt;/strong&gt;, &lt;strong&gt;Symbol, BigInt&lt;/strong&gt;, &lt;strong&gt;Boolean, undefined&lt;/strong&gt; and &lt;strong&gt;null&lt;/strong&gt;. Additionally, there are two composite data types, &lt;strong&gt;objects&lt;/strong&gt; and &lt;strong&gt;functions&lt;/strong&gt;, which are used to group together and organize data.&lt;/p&gt;

&lt;p&gt;The question you must ask yourselves before you begin is this: &lt;em&gt;"Why is it important to know the type of a value?"&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Importance of Knowing the Type of a Value
&lt;/h2&gt;

&lt;p&gt;It is important to know the type of a value in JavaScript because the type determines how the value can be used. Different types have different characteristics and methods that can be used on them, and using a method or operator on the wrong type of value can lead to unexpected results or errors.&lt;/p&gt;

&lt;p&gt;Additionally, knowing the type of a value can be useful when you are debugging your code and trying to understand why it is not working as expected. Understanding this kind of equality helps prevent bugs! You will often need to know when you're dealing with the same value, and when you're dealing with two different values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_VddlohO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zawl5c0tynwruxgecdl9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_VddlohO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zawl5c0tynwruxgecdl9.png" alt="Printing types of values" width="880" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✏️ Let's sketch a diagram for this code snippet:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--thc3is_n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1evmurlydvgr3789xg9h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--thc3is_n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1evmurlydvgr3789xg9h.png" alt="Diagram represents variables pointing their values" width="422" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Kinds of Equality
&lt;/h2&gt;

&lt;p&gt;In JavaScript, there are three types of value equality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Strict Equality: &lt;code&gt;a === b&lt;/code&gt; (triple equals).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Same Value Equality: &lt;code&gt;Object.is(a, b)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Loose Equality: &lt;code&gt;a == b&lt;/code&gt; (double equals).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2.1. Strict Equality: a === b
&lt;/h2&gt;

&lt;p&gt;Strict equality, also known as "identity" or "triple equals" &lt;code&gt;===&lt;/code&gt;, checks both the value and the type of the operands. There is also a corresponding opposite &lt;code&gt;!==&lt;/code&gt; operator.&lt;/p&gt;

&lt;p&gt;The strict equality operator uses the &lt;a href="https://262.ecma-international.org/7.0/#sec-strict-equality-comparison"&gt;Strict Equality Comparison algorithm&lt;/a&gt;, and checks operator evaluates to &lt;code&gt;true&lt;/code&gt; when both values are of the same type and hold the same value.&lt;/p&gt;

&lt;p&gt;Let's examine the examples in the code snippet:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--byXdlX5Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksc2y4zt20rrkf7djfmp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--byXdlX5Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksc2y4zt20rrkf7djfmp.png" alt="Examples of Strict Equality" width="880" height="959"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2.2. Same Value Equality: Object.is(a, b)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Object.is()&lt;/code&gt; method was introduced in ECMAScript 2015 (ES6) specification, in contrast to strict equality operator &lt;code&gt;===&lt;/code&gt;, uses &lt;a href="https://262.ecma-international.org/7.0/#sec-samevalue"&gt;Same Value Comparison algorithm&lt;/a&gt;. Despite Object in the method name, &lt;code&gt;Object.is()&lt;/code&gt; is not specific to objects. It can compare any two values, whether they are objects or not!&lt;/p&gt;

&lt;p&gt;In JavaScript, &lt;code&gt;Object.is(a, b)&lt;/code&gt; tells us if a and b are the same value.&lt;/p&gt;

&lt;p&gt;Let's examine the examples in the code snippet:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fh3p-VpV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rezf21f1dlssv9cfvhy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fh3p-VpV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rezf21f1dlssv9cfvhy.png" alt="Examples of Same Value Equality" width="880" height="982"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🤔 Before moving on to the Loose Equality, did anything catch your attention? Please continue reading to understand what it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Same Value Equality vs. Strict Equality
&lt;/h2&gt;

&lt;p&gt;If you have carefully studied the code snippets I have provided for Strict Equality and Same Value Equality above, you will notice that we always get the same results, except for &lt;strong&gt;two rare cases&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;NaN === NaN&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, although they are the same value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;-0 === 0&lt;/code&gt; and &lt;code&gt;0 === -0&lt;/code&gt; are &lt;code&gt;true&lt;/code&gt;, although they are different values.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;✅ Although Same Value Equality printed the opposite, Strict Equality's answer was accepted as valid.&lt;/p&gt;

&lt;p&gt;📌 Ensuring developers could detect &lt;code&gt;NaN&lt;/code&gt; this way was &lt;a href="https://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values/1573715#1573715"&gt;one of the original reasons&lt;/a&gt; for making &lt;code&gt;NaN === NaN&lt;/code&gt; return &lt;code&gt;false&lt;/code&gt;! This was decided before JavaScript even existed.&lt;/p&gt;

&lt;p&gt;📌 In regular math, there is no such concept as "minus zero," but it exists in floating-point math for &lt;a href="https://softwareengineering.stackexchange.com/questions/280648/why-is-negative-zero-important/280708#280708"&gt;practical reasons&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.3. Loose Equality: a == b
&lt;/h2&gt;

&lt;p&gt;In JavaScript, loose equality (also known as "double equals" or "abstract equality") is a comparison operator that checks only the value of the operands, not their type. This means that two values of different types can be considered equal if they have the same value when converted to a common type.&lt;/p&gt;

&lt;p&gt;The rules of loose equality are widely acknowledged as an early bad design decision of JavaScript. Many coding standards prohibit the use of &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;!=&lt;/code&gt; in code altogether.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EOWYK42i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xhoi44axe4snnkl08zk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EOWYK42i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xhoi44axe4snnkl08zk.png" alt="Examples of Loose Equality" width="880" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Summary:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In JavaScript, there are three types of value equality: Same Value Equality &lt;code&gt;Object.is()&lt;/code&gt;, Strict Equality &lt;code&gt;===&lt;/code&gt; and Loose Equality &lt;code&gt;==&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are two rare differences between Same Value Equality and Strict Equality : &lt;code&gt;NaN === NaN&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;-0 === 0&lt;/code&gt; and &lt;code&gt;0 === -0&lt;/code&gt; are &lt;code&gt;true&lt;/code&gt;, &lt;strong&gt;consider them as exceptions to the rule&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In most of the situations, the strict equality operator is a good way to compare values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also &lt;code&gt;Object.is()&lt;/code&gt; is useful as a functional way to compare values, for example in functional programming.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Follow me on &lt;a href="https://github.com/eminebozdag"&gt;&lt;strong&gt;Github&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/eminebozdag/"&gt;&lt;strong&gt;LinkedIn&lt;/strong&gt;&lt;/a&gt;, see you in the next chapter ! 👋🏻&lt;/p&gt;

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