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" === true);

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

console.assert(Infinity === Infinity === true);

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

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

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

to test if a value is NaN see

Triple Equal on objects, reference equality

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);

No Auto Type Conversion

Triple equal does not convert Value Types implicitly.

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

JavaScript. Boolean

JavaScript. Test Equality