JS: Triple Equal Operator
Compare Primitives
Triple Equal can compare all Primitive Values except NaN.
"x" === "x"
3 === 3
3 === 3.0
Infinity === Infinity
(NaN === NaN) === false
to test if a value is NaN use
Test Object Equality by Triple Equal
Objects (including Array) cannot be compared with triple equal.
When a object is assigned to a variable, the variable holds a reference to the object.
If 2 variables hold the same reference, they are equal.
const x = { "a": 3 }; const y = x; // x and y holds the same reference console.log(x === y);
but 2 objects with same property values, are still considered different if they have different reference.
// Objects cannot be compared with triple equal console.log(([] === []) === false); console.log(({} === {}) === false); console.log(({ "a": 3 } === { "a": 3 }) === false);
to test equality of objects, see JS: Test Object Equality π
No Auto Type Conversion
Triple equal does not convert Value Types implicitly.
("0" === 0) === false