Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate20 minLESSON 20 / 34

Equality & structural comparison

01

Understand the idea

Two settings print exactly the same fields, yet === says they differ. Their contents match; their identities do not.

Choose what matching means

A settings form needs to know whether the user changed anything. Comparing the saved object with a new draft using === asks whether they are the very same object, which is a different question. Decide what “unchanged” means before picking a comparison.

Strict and loose equality

Strict equality does not convert operand types. For objects, it checks whether both references identify the same object. Separately created or objects are different even when all their visible fields match. Loose equality adds coercion rules; it does not solve structural comparison.

Different meanings of equal
RuleDifferent types?Two separate matching objects?NaN with NaN?
===No coercion; normally falseFalseFalse
==May coerce before comparingFalseFalse
Object.isNo coercionFalseTrue; also distinguishes +0 and -0
SameValueZero (Set / Map)No coercionDistinct identitiesTrue; treats +0 and -0 as equal
Explicit field comparisonWhatever your field rules requireTrue if the chosen fields matchDepends on your chosen field rule

Special numeric values

Numbers have two useful edge cases. NaN === NaN is false, while positive and negative zero compare equal. Object.is treats as equal to itself and distinguishes the two zeros. These rules matter for some low-level comparisons, but they still do not compare object contents.

NaN is unusual: it does not equal itself under ===. Use Number.isNaN when checking for that numeric result. Object.is treats NaN as equal to itself but distinguishes positive and negative zero. Set and Map use another rule called SameValueZero, which treats NaN as equal and both zeros as the same.

Object identity versus contents

Structural comparison needs a definition. For a settings form, comparing theme and pageSize may be enough. For richer values, you must decide how to handle missing versus fields, dates, arrays whose order may matter, maps, nested objects, and cycles. There is no single tiny comparison that silently answers all those questions correctly.

Compare the fields that matter

Stringifying both values is tempting but does not supply those decisions. Property order and unsupported values can change or erase information. Prefer a small explicit comparison for known data, or an established equality utility with behavior you have checked for your domain.

Structural comparison means checking contents. The example deliberately compares theme and pageSize, because those are this form's contract. A general deep comparison must define how it handles nested values, missing fields, dates, arrays, and cycles. JSON.stringify is not a substitute: serialization can omit data and may distinguish objects because their key order differs.

02

Mental model

FOLLOW THE VALUE

saved and draft
different identities
=== false

Equal-looking records can be separate objects.

01 / 03Trace the transformation

03

Live example

The identity check is false, the two-field comparison is true, and .is demonstrates its special handling of .

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Compare the meaningful fields

The domain comparison should require both settings fields to match.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

false
true
true

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does === compare for objects?
    02Which comparison treats NaN as equal to itself?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson