You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The rules of Loose Equality (also called “abstract equality”) are arcane and confusing. They are widely acknowledged as an early bad design decision. Many coding standards prohibit the use of == and != in code altogether.
Recap
JavaScript has several kinds of equality. They include Same Value Equality, Strict Equality, and Loose Equality.
Same Value Equality, or Object.is(a, b), matches the concept of the sameness of values that we introduced in the previous module.
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.
When we draw a diagram of values and variables, the same value cannot appear twice on it. Object.is(a, b) is true when variables a and b point to the same value on our diagram.
Same Value Equality is the easiest to explain, which is why we started with it. However, it’s verbose and a bit annoying to write.
In practice, you will use Strict Equality, or a === b, most often. It is equivalent to the Same Value Equality except for two rare special cases:
NaN === NaN is false, even though they are the same value.
0 === -0 and -0 === 0 is true, but they are different values.
You can check whether x is NaN using Number.isNaN(x).
Loose Equality (==) is a set of arcane rules and is often avoided.
The text was updated successfully, but these errors were encountered:
06. Equality of Values
Kinds of Equality
Three kinds of equality:
Same Value Equality: Object.is(a, b)
In JavaScript, Object.is(a, b) tells us if a and b are the same value:
This is called Same Value Equality.
Check Your Intuition
what about these question:
Here are the answers:
But What About Objects?
look at this code snippet:
what is the answer?
the answer is:
Strict Equality: a === b
Strict Equality example:
Same Value Equality vs Strict Equality
Same Value Equality — Object.is(a, b) — has a direct meaning in our mental model. It corresponds to the idea of “the same value” in our universe.
In almost all cases, the same intuition works for Strict Value Equaliy too. For example, 2 === 2 is true because 2 always “summons” the same value.
Consider the cases below as exceptions to the rule:
First Special Case: NaN
Remember that NaN === NaN is always false:
You may be confusing about this.The reason for NaN === NaN being false is largely historical so I suggest to acccept it as a fact of life.
Instead, here’s a few ways (they all work!) to check if size is NaN:
Second Special Case: -0
Both 0 === -0 and -0 === 0 are always true:
However, 0 is a different value from -0:
Loose Equality
The rules of Loose Equality (also called “abstract equality”) are arcane and confusing. They are widely acknowledged as an early bad design decision. Many coding standards prohibit the use of == and != in code altogether.
Recap
The text was updated successfully, but these errors were encountered: