HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

What's the difference between Object.is() and the triple equals operator in JavaScript?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
objectjavascriptwhattripleandequalsbetweendifferencetheoperator

Problem

If you want to check equality in JavaScript, there are two comparison operators, which are explained in depth in a previous article.
Very briefly, the double equals operator (==) only compares value whereas the triple equals operator (===) compares both value and type. But there is also a third option, Object.is(), which behaves the same as the triple equals operator with the exception of NaN and +0 and -0.
Here are some examples for additional clarity:

Solution

{} === {}; // false
Object.is({}, {}); // false

1 === 1; // true
Object.is(1, 1); // true

+0 === -0; // true
Object.is(+0, -0); // false

NaN === NaN; // false
Object.is(NaN, NaN); // true


Here are some examples for additional clarity:

Code Snippets

{} === {}; // false
Object.is({}, {}); // false

1 === 1; // true
Object.is(1, 1); // true

+0 === -0; // true
Object.is(+0, -0); // false

NaN === NaN; // false
Object.is(NaN, NaN); // true

Context

From 30-seconds-of-code: object-is-triple-equals

Revisions (0)

No revisions yet.