Equality & structural comparison
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.
| Rule | Different types? | Two separate matching objects? | NaN with NaN? |
|---|---|---|---|
| === | No coercion; normally false | False | False |
| == | May coerce before comparing | False | False |
| Object.is | No coercion | False | True; also distinguishes +0 and -0 |
| SameValueZero (Set / Map) | No coercion | Distinct identities | True; treats +0 and -0 as equal |
| Explicit field comparison | Whatever your field rules require | True if the chosen fields match | Depends 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.
Mental model
FOLLOW THE VALUE
Equal-looking records can be separate objects.
01 / 03Trace the transformation
Live example
The identity check is false, the two-field comparison is true, and .is demonstrates its special handling of .
Your console output appears here.
Guided challenge
Compare the meaningful fields
The domain comparison should require both settings fields to match.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
false true true
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.