Equality and Comparison

Beginner · 12 min

How `==` and `===` differ, what `Object.is` fixes that neither can, the SameValueZero algorithm `Map`/`Set` use, and why `==` surprises trace to coercion.

Before thisValues, Types, and Coercion

Mr Code Potato · 12 min

Equality and Comparison

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Mr Code PotatoReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `==` (loose equality) converts both operands to a common type before comparing, which is why surprises like `0 == false` and `"" == 0` are both `true` — `===` (strict equality) skips that conversion entirely and compares type and value directly.
  • For objects — including arrays — both `==` and `===` compare identity, not contents, so two separately-built objects with identical data are never equal to each other, only to themselves.
  • `Object.is()` behaves like `===` with two deliberate exceptions: `Object.is(NaN, NaN)` is `true` (where `NaN === NaN` is `false`), and `Object.is(+0, -0)` is `false` (where `+0 === -0` is `true`).
  • SameValueZero is the equality algorithm `Map`, `Set`, and `Array.prototype.includes` actually use internally — it matches `Object.is` on `NaN`, but unlike `Object.is` it treats `+0` and `-0` as equal, same as `===`.
  • Default to `===` in everyday code. Reach for `Object.is` only when `NaN` or signed-zero distinctions genuinely matter, and treat `==` as having exactly one defensible use: `x == null`, which catches both `null` and `undefined` in a single check.

The question equality has to answer

“Are these two things the same” sounds like it should have one obvious answer, but JavaScript actually has to answer a more specific question every time: same in what sense — same value after converting types to match, same value with no conversion at all, or same object in memory? Those are three genuinely different checks, and JavaScript gives you a different operator for each. Picking the wrong one is one of the most common sources of bugs that look mysterious until you know which kind of “same” you actually meant.

Loose equality: same value, after coercion

== compares two values after converting them to a shared type if they don’t already match. That conversion is exactly the coercion covered in the previous lesson — equality isn’t a separate set of rules, it’s ordinary coercion feeding into a comparison. That’s why 0 == false is true (both become 0), why "" == 0 is true (the string becomes 0), and why these results feel arbitrary if you haven’t internalized the coercion rules first: they’re not arbitrary, they’re just coercion doing what it always does, in a spot where the conversion happens to be surprising.

Strict equality: same value, no conversion

=== skips coercion entirely. If the two operands aren’t already the same type, the answer is false, full stop — no conversion attempted. For primitives, this means comparing the actual value: 5 === 5 is true, 5 === "5" is false because a number is never the same type as a string. For objects, === (like ==) compares identity, not contents — two arrays or objects with matching data are still two different objects in memory, so they’re never === to each other, only to themselves. That’s why === is the right default almost everywhere: it never surprises you with a hidden conversion, and when it returns true you know exactly what that means.

Object.is: fixing the two cases === gets “wrong”

=== is consistent, but it inherits two behaviors from the floating-point spec that are occasionally the opposite of what you want: NaN === NaN is false (no NaN is ever equal to anything), and +0 === -0 is true (signed zero is invisible to ===). Object.is() exists specifically to flip both of those: Object.is(NaN, NaN) is true, and Object.is(+0, -0) is false. It’s not a replacement for === in general — just the tool for the specific moments those two edge cases matter, like writing a custom data structure that needs to treat NaN as a normal, comparable value.

The video walks through exactly this progression — ==, then ===, then where Object.is diverges from both, building from a concrete “why did this lookup fail” bug rather than a dry list of rules — which is why it’s the embed here over a shorter clip that only covers == versus === and stops.

SameValueZero: what your collections actually use

There’s a fourth algorithm working quietly in the background: SameValueZero, which Map keys, Set membership, and Array.prototype.includes use internally. It splits the difference between the other two — NaN is treated as equal to itself (like Object.is), but +0 and -0 are treated as equal to each other (like ===). You don’t call it directly, but it explains behavior that otherwise looks inconsistent: a Set correctly treats two NaN values as duplicates, even though NaN === NaN would tell you they’re different.

Where this fits

The practical takeaway is simple even though the landscape has four algorithms in it: use === by default, know why == surprises people so you can spot it in other people’s code, and keep Object.is and SameValueZero in your back pocket for the rare moments NaN or signed zero actually matter. This lesson closes out the “values” half of the roadmap’s foundations — from here, variables, scope, and hoisting shifts from what values are to where they live.

Check yourself

  1. 01Why is `0 == ""` true, but `0 === ""` false?

    `==` converts both sides to a common type before comparing — here, the empty string gets coerced to the number `0`, so it's comparing `0` to `0`, which is true. `===` never coerces; it compares a number and a string directly, sees they're different types, and returns `false` without looking any further.

  2. 02Why is `[1, 2, 3] === [1, 2, 3]` false even though both arrays look identical?

    Both `==` and `===` compare objects (arrays included) by reference, not by contents. Each array literal creates a brand new object in memory, so even with matching elements, they're two distinct objects — equal only to themselves, never to a separately-created object with the same data.

  3. 03Name one case where `Object.is()` gives a different answer than `===` — why does that distinction exist?

    `Object.is(NaN, NaN)` returns `true`, while `NaN === NaN` returns `false`. `===` follows the IEEE 754 rule that `NaN` is never equal to anything, including itself. `Object.is` was added specifically to give JavaScript a comparison that treats `NaN` as equal to itself, which ordinary equality can't do without breaking the floating-point spec it otherwise follows.

  4. 04What is SameValueZero, and what's one built-in that actually uses it instead of `===` or `Object.is`?

    SameValueZero is the equality check JavaScript's own collections use internally — it treats `NaN` as equal to itself (like `Object.is`) but treats `+0` and `-0` as equal (like `===`, unlike `Object.is`). `Map` keys, `Set` membership, and `Array.prototype.includes` all use it, which is why `[NaN].includes(NaN)` is `true` even though `[NaN].indexOf(NaN)` returns `-1` (`indexOf` uses `===`).

  5. 05Is there ever a legitimate reason to use `==` instead of `===`?

    One: `x == null` checks whether `x` is either `null` or `undefined` in a single comparison, because `==` treats those two as loosely equal to each other (and to nothing else). Outside that specific idiom, `==`'s coercion rules produce enough surprising results that `===` is the safer default almost everywhere.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark