Comparisons in JavaScript – From === to Object.is and the Difference Between Primitives and References

Comparing values is essential for decision-making in JavaScript programs. This article explores the nuances of strict equality (===), identity vs structural comparison, and special cases like NaN and -0. It also explains how object and function comparisons work and why JavaScript doesn’t support structural equality natively. Understanding these distinctions is key to writing reliable code.

comparisonstrict-equalityObject.isreference-value

~2 min read • Updated Oct 22, 2025

Introduction


In programming, comparisons help us make decisions. JavaScript offers several mechanisms for comparing values, each with its own behavior. This article explores the subtleties of equality and identity in JS.


Equal…ish


The most common comparison asks: “Is value X the same as value Y?” But in JS, “same” can mean different things depending on context. The === operator, known as strict equality, checks both value and type — without coercion:


3 === 3.0;       // true
"yes" === "yes"; // true
42 === "42";     // false
true === 1;      // false
null === undefined; // false

While === avoids type coercion, it has quirks — especially with NaN and -0.


Comparing NaN and -0


NaN === NaN; // false
0 === -0;    // true

To compare these values accurately, use:


Number.isNaN(NaN);       // true
Object.is(-0, 0);        // false
Object.is(NaN, NaN);     // true

Object.is(..) is like a “quadruple-equals” — a truly strict comparison that doesn’t lie.


Comparing Objects and Functions


Object comparisons in JS use reference identity, not structure or content:


[1, 2, 3] === [1, 2, 3]; // false
{ a: 42 } === { a: 42 }; // false
(x => x * 2) === (x => x * 2); // false

Even if two objects look identical, they are different if they are separate instances.


Reference Identity in Practice


var x = [1, 2, 3];
var y = x;
y === x; // true
y === [1, 2, 3]; // false

x and y point to the same array, so they are equal. But comparing to a new array fails, even if the contents match.


Structural Equality? Not in JS


JavaScript does not provide built-in structural equality for objects. You must implement it manually — and it’s complex. For example, comparing two functions structurally is nearly impossible due to closures and context.


Conclusion


Comparisons in JavaScript are nuanced. === works well for most cases, but for special values like NaN and -0, use Object.is(..) or Number.isNaN(..). For objects and functions, JS compares references, not structure. Understanding these distinctions helps you write more accurate and predictable code.


Written & researched by Dr. Shahin Siami