JavaScript has four different equality comparison operations.
It returns true
if both operands belong to the same Type and are the same value.
Note: the value of an object is a reference.
You can use this comparison algorithm via Object.is
(ECMAScript 6).
Examples:
Object.is(1, 1); // true
Object.is(+0, -0); // false
Object.is(NaN, NaN); // true
Object.is(true, "true"); // false
Object.is(false, 0); // false
Object.is(null, undefined); // false
Object.is(1, "1"); // false
Object.is([], []); // false
This algorithm has the properties of an equivalence relation:
Object.is(x, x)
is true
, for any value x
Object.is(x, y)
is true
if, and only if, Object.is(y, x)
is true
, for any values x
and y
.Object.is(x, y)
and Object.is(y, z)
are true
, then Object.is(x, z)
is also true
, for any values x
, y
and z
.It behaves like SameValue, but considers +0
and -0
to be equal.
You can use this comparison algorithm via Array.prototype.includes
(ECMAScript 7).
Examples:
[1].includes(1); // true
[+0].includes(-0); // true
[NaN].includes(NaN); // true
[true].includes("true"); // false
[false].includes(0); // false
[1].includes("1"); // false
[null].includes(undefined); // false
[[]].includes([]); // false
This algorithm still has the properties of an equivalence relation:
.includes(x)
is true
, for any value x
.includes(y)
is true
if, and only if, [y].includes(x)
is true
, for any values x
and y
..includes(y)
and [y].includes(z)
are true
, then .includes(z)
is also true
, for any values x
, y
and z
.