Skip to the workspace
Pebellslearn by building
Equality Comparisons  ›  Lesson

The Four Equality Algorithms

isLooselyEqual 20 min

javascript / The Four Equality Algorithms

Explore each scene at your own pace.

01 · The Four Equality Algorithms

The Four Equality Algorithms

The Four Equality Algorithms

IsLooselyEqual, IsStrictlyEqual, SameValue and SameValueZero are specification algorithms for different comparison rules. They are not four global JavaScript functions. Operators and APIs expose these rules for different purposes.

Before saying two things are equal, choose the rule: convert types, keep types, distinguish zero signs, or group repeated NaN values.

Connect the specification names to code

The roadmap's names describe rules used by the language. JavaScript does not provide global functions named isLooselyEqual or isStrictlyEqual. == invokes the loose rule, === invokes the strict rule, Object.is exposes SameValue behavior, and APIs such as Set membership, Map keys and Array.prototype.includes use SameValueZero.

Pair == === Object.is SameValueZero
4, '4' true false false false
NaN, NaN false false true true
0, -0 true true false true
null, undefined true false false false
Two separately created {} objects false false false false
Walk through the modelThe Four Equality AlgorithmsAuthored snapshots of the example; use Run below to test the code.
loose== → IsLooselyEqual
strict=== → IsStrictlyEqual
The language exposes comparison algorithms through operators and APIs.
1 of 3
javascript

The SameValueZero helper is a teaching implementation for these ordinary JavaScript values: strict equality handles the ordinary matching cases, and the second part accepts two NaN operands. A Set can therefore contain one NaN entry even after several additions. includes can find NaN, while indexOf uses strict-equality-style comparison and cannot find it. None of these operations performs recursive object-field comparison.

Do not implement loose equality as Number(a) === Number(b)

The loose algorithm has branches for operand types, including a special null/undefined pairing and object-to-primitive conversion. The result of Number(null) does not imply that null == 0. Object conversion can call user-defined conversion behavior; comparisons can consequently have effects beyond a simple numeric check. Prefer strict equality unless a deliberate, documented conversion is required.

Choose the product behavior

Use strict equality for a selected string ID compared with another string ID. Use Set or includes when membership should group NaN and zero signs under SameValueZero. Use Object.is when signed zero must remain distinguishable. If the product needs structural record equality, define which fields matter and compare them explicitly; changing the equality operator will not invent that policy.

Independent check

The grader cannot send a literal NaN or negative zero through ordinary JSON reliably. Instead, it supplies token strings; the task converts those tokens to values before comparing. This is an input encoding for the exercise, not a new equality rule. Explain why handling the tokens as strings would incorrectly make the 'NaN' case look like an ordinary string comparison.

Change the inputs and see the result

This lab executes the displayed operations with real JavaScript values. Change one input, predict which output changes, then compare the result.

html
Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.