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

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:
{} === {}; // 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
Image credit: Jonathan Sanchez on Unsplash
Recommended snippets
What is the difference between JavaScript's for...in, for...of and forEach?
JavaScript, Article
Learn the differences between the three most commonly used iteration methods offered by JavaScript, which often confuse beginners and veterans alike.
What's the difference between undeclared, undefined and null in JavaScript?
JavaScript, Article
JavaScript has three different empty states for variables. Learn their differences and how you can check for each one.
What is the difference between JavaScript's equality operators?
JavaScript, Article
Learn all you need to know about the differences between JavaScript's double equals and triple equals operators.