JS: Triple Equal Operator

By Xah Lee. Date: . Last updated: .

Compare primitives

Triple Equal can compare all Primitive Values except NaN.

console.assert("x" === "x");

console.assert(3 === 3);
console.assert(3 === 3.0);

console.assert(Infinity === Infinity);

console.assert(-0 === +0);

// s------------------------------

console.assert((NaN === NaN) === false);

to test if a value is NaN see

No Auto Type Conversion

console.assert(("0" === 0) === false);

Triple equal on objects (reference identity comparison)

When a object is assigned to a variable, the variable holds a reference to the object.

If 2 objects hold the same reference, triple equal return true.

const x = { "a": 3 };
const y = x;
// x and y holds the same reference
console.assert(x === y);

Triple equal operator on objects with same property values usually return false, because they do not hold the same reference.

// triple equal operator on objects, does not work the way you think

console.assert(([] === []) === false);

console.assert(({} === {}) === false);

console.assert(({ "a": 3 } === { "a": 3 }) === false);