How does JavaScript compare objects with `===`?
Quick answer
For objects, === checks whether both sides refer to the same object, not whether their properties match. So two separately created objects with identical contents compare as false, while two variables pointing to one object compare as true. For primitive values, strict equality compares values without converting types; for example, 3 === 3 is true, but 3 === "3" is false. [S1]
The question, the details, the next step.
A practical checklist
Identify whether each operand is an object or a primitive value.
For objects, check whether both variables refer to the same object or to separately created objects.
Use
===when the question is whether object references are identical.If matching contents are the goal, decide which properties count and compare those explicitly.
For primitive comparisons, check that the values have the same type and consider numeric edge cases such as
NaN.
A few more useful answers
Does `===` compare every property of two objects?
No. For objects, it checks whether both operands refer to the same object, not whether their properties match. [S1]
Why is `3 === "3"` false?
Strict equality does not convert values to make their types match. The number 3 and the string "3" are different types, so this comparison is false. [S1]
Can `x === x` be false?
Yes, if x is NaN: NaN is unequal to itself under strict equality. Positive and negative zero, by contrast, compare equal. [S1]
Sources
References used for this guide.
- [S1] Equality comparisons and sameness — JavaScript | MDN developer.mozilla.org · Checked
