Course contentsShow
← JavaScript
Lesson 59 of 2,7232. Values and TypesFree lesson

Object.is for Same-Value Equality

Learn Object.is as an alternative that treats NaN as equal to itself and distinguishes -0 from +0.

Object.is for Same-Value Equality

You'll learn how Object.is() provides the most precise equality check in JavaScript, fixing quirks with NaN and signed zeros.

The Most Accurate Equality

You already know that === is JavaScript's strict equality operator. It's reliable for most comparisons, but it has two odd edge cases you learned about: NaN is never equal to itself, and -0 equals +0.

Object.is() is a method that performs same-value equality, treating these special cases more intuitively:

  • NaN is considered equal to NaN
  • -0 and +0 are considered different values

Think of Object.is() as asking "are these truly the same value?" rather than "do they compare as equal?" It's like using a precision instrument instead of a regular ruler.

When to Use Object.is()

// NaN comparison
NaN === NaN;           // false (weird!)
Object.is(NaN, NaN);   // true (sensible!)

// Signed zero comparison
-0 === +0;             // true (masks a difference)
Object.is(-0, +0);     // false (reveals the difference)

// Everything else works like ===
Object.is(5, 5);       // true
Object.is("hi", "hi"); // true
Object.is(true, true); // true

When Does This Matter?

Most of the time, === is perfectly fine. Use Object.is() when you specifically need to:

  • Detect NaN values reliably
  • Distinguish between positive and negative zero (rare, but important in some mathematical contexts)

For everyday comparisons of strings, numbers, booleans, and objects, stick with ===.

Key Takeaway: Object.is() provides the most mathematically correct equality check in JavaScript, treating NaN as equal to itself and distinguishing -0 from +0.