Learning on Web Dev Open is free for all.

Systems Thinking > What a variable actually holdsThree kinds of equal
Phase 03What a variable actually holds135 of 434

Three kinds of equal

Loose, strict, and Object.is, plus the one your framework actually uses, which is none of them.

Concept11 minAI pair

Loose equality coerces, and the rules are genuinely arcane. You need two of them: null == undefined is true and nothing else is loosely equal to either, which makes x == null a legitimate and readable null-or-undefined check. Everything else, use ===.

Strict equality on objects compares references, not contents. Two arrays with identical elements are never ===. This is not a flaw, it is the only comparison that can be done in constant time, but it means "did this change?" is a question you have to answer structurally, either by never mutating in place or by writing a comparison that knows your shape.

Object.is differs from === in exactly two places: NaN is Object.is-equal to itself, and +0 is not Object.is-equal to -0. Those two rows are why React uses Object.is rather than === for its bail-out check, and why a state value of NaN does not cause an infinite re-render loop.

You should now be able to

  • Explain what === does with two objects
  • Name the two cases where Object.is differs from ===
Ask the community

Loading…