JavaScript Strict Equality and Object Identity
Quick answer
Strict equality === compares values without coercing different language types into a common type. Strict inequality !== negates that test.
How it works
For ordinary primitives, compare same-type values; Number has two important exceptions: NaN never equals itself and positive and negative zero compare equal. Objects compare by identity, not by the contents of their properties. Object.is uses different rules for NaN and signed zero; this is a preview of later object APIs.
console.log(5 === "5");console.log(5 !== "5");console.log(0 === -0);Expected output in Node.js:
falsetruetrueDifferent types do not match; both zero signs compare equal.
const a = { score: 7 };const b = { score: 7 };const alias = a;console.log(a === b);console.log(a === alias);console.log(NaN === NaN);Expected output in Node.js:
falsetruefalseOnly the alias holds the same object; NaN is not strictly equal to itself.
Common mistakes
Equality is not a deep object comparison. Use a purpose-specific comparison for data structures. Do not test for NaN with === NaN; use Number.isNaN.
Practice
Compare two JSON primitive values without implicit type conversion.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Strict Equality and Object Identity — Knowledge Check
Apply the taught concept and reason about the result.
What is 7 === "7"?
What is 0 === -0?
What is NaN === NaN?
Two separately created objects have the same properties. What does strict equality test?
Which expression distinguishes the two signed zeros?