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.
✏️ Let's sketch a diagram for this code snippet:
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, 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:
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. 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:
🤔 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.
NaN === NaN
isfalse
, although they are the same value.-0 === 0
and0 === -0
aretrue
, 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 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.
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.
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
isfalse
,-0 === 0
and0 === -0
aretrue
, 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 and LinkedIn, see you in the next chapter ! 👋🏻
Top comments (0)