Equality of Values in Javascript
Tags: javascript
Published: 2022-12-15 20:41:49

JavaScript has several built-in data types that are used to represent different kinds of values. The primitive data types in JavaScript include **Number**, **String**, **Symbol, BigInt**, **Boolean, undefined** and **null**. Additionally, there are two composite data types, **objects** and **functions**, which are used to group together and organize data.


The question you must ask yourselves before you begin is this: _"Why is it important to know the type of a value?"_

## 1. The Importance of Knowing the Type of a Value

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.

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.


![Printing types of values](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zawl5c0tynwruxgecdl9.png)

✏️ Let's sketch a diagram for this code snippet:


![Diagram represents variables pointing their values](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1evmurlydvgr3789xg9h.png)


## 2. Kinds of Equality

In JavaScript, there are three types of value equality:

- Strict Equality: `a === b` (triple equals).

- Same Value Equality: `Object.is(a, b)`.

- Loose Equality: `a == b` (double equals).

## 2.1. Strict Equality: a === b

Strict equality, also known as "identity" or "triple equals" `===`, checks both the value and the type of the operands. There is also a corresponding opposite `!==` operator.

The strict equality operator uses the [Strict Equality Comparison algorithm](https://262.ecma-international.org/7.0/#sec-strict-equality-comparison), and checks operator evaluates to `true` when both values are of the same type and hold the same value.

Let's examine the examples in the code snippet:



![Examples of Strict Equality](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksc2y4zt20rrkf7djfmp.png)

## 2.2. Same Value Equality: Object.is(a, b)

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

In JavaScript, `Object.is(a, b)` tells us if a and b are the same value.

Let's examine the examples in the code snippet:


![Examples of Same Value Equality](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rezf21f1dlssv9cfvhy.png)

🤔 Before moving on to the Loose Equality, did anything catch your attention? Please continue reading to understand what it is.

## Same Value Equality vs. Strict Equality

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 **two rare cases**.

1. `NaN === NaN` is `false`, although they are the same value.

2. `-0 === 0` and `0 === -0` are `true`, although they are different values.

✅ Although Same Value Equality printed the opposite, Strict Equality's answer was accepted as valid.

📌 Ensuring developers could detect `NaN` this way was [one of the original reasons](https://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values/1573715#1573715) for making `NaN === NaN` return `false`! This was decided before JavaScript even existed.

📌 In regular math, there is no such concept as "minus zero," but it exists in floating-point math for [practical reasons](https://softwareengineering.stackexchange.com/questions/280648/why-is-negative-zero-important/280708#280708).

## 2.3. Loose Equality: a == b
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.

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


![Examples of Loose Equality](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xhoi44axe4snnkl08zk.png)

## 3. Summary:

- In JavaScript, there are three types of value equality: Same Value Equality `Object.is()`, Strict Equality `===` and Loose Equality `==`.


- There are two rare differences between Same Value Equality and Strict Equality : `NaN === NaN` is `false`, `-0 === 0` and `0 === -0` are `true`, **consider them as exceptions to the rule**.
 

- In most of the situations, the strict equality operator is a good way to compare values.

- Also `Object.is()` is useful as a functional way to compare values, for example in functional programming.

Follow me on [**Github**](https://github.com/eminebozdag) and [**LinkedIn**](https://www.linkedin.com/in/eminebozdag/), see you in the next chapter ! 👋🏻