JavaScript: Triple Equal Operator
Compare Primitives
Triple Equal can compare most Primitive Values such as string, number, Symbol, undefined, null, except NaN.
console.log("x" === "x"); console.log(3 === 3); console.log(3 === 3.0); console.log(Infinity === Infinity);
NaN cannot be compared with triple equal.
console.log((NaN === NaN) === false);
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 JavaScript: Test Object Equality π
No Auto Type Convert Conversion
Triple equale does not convert Value Types implicitly.
console.log(([] === "") === false); console.log(("" === {}) === false); console.log(("0" === 0) === false); console.log(("" === 0) === false); console.log((1 === true) === false);