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 xObject.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:
[x].includes(x) is true, for any value x[x].includes(y) is true if, and only if, [y].includes(x) is true, for any values x and y.[x].includes(y) and [y].includes(z) are true, then [x].includes(z) is also true, for any values x, y and z.It behaves like SameValue, but
+0 and -0 to be equal.NaN different than any value, including itselfYou can use this comparison algorithm via the === operator (ECMAScript 3).
There is also the !== operator (ECMAScript 3), which negates the result of ===.
Examples:
1 === 1; // true
+0 === -0; // true
NaN === NaN; // false
true === "true"; // false
false === 0; // false
1 === "1"; // false
null === undefined; // false
[] === []; // false
This algorithm has the following properties:
x === y is true if, and only if, y === xistrue, for any valuesxandy`.x === y and y === z are true, then x === z is also true, for any values x, y and z.But is not an equivalence relation because
NaN is not reflexive: NaN !== NaNIf both operands belong to the same Type, it behaves like the Strict Equality Comparison.
Otherwise, it coerces them as follows:
undefined and null are considered to be equalIf there was a coercion, the coerced values are compared recursively. Otherwise the algorithm returns false.
You can use this comparison algorithm via the == operator (ECMAScript 1).
There is also the != operator (ECMAScript 1), which negates the result of ==.
Examples:
1 == 1; // true
+0 == -0; // true
NaN == NaN; // false
true == "true"; // false
false == 0; // true
1 == "1"; // true
null == undefined; // true
[] == []; // false
This algorithm has the following property:
x == y is true if, and only if, y == x is true, for any values x and y.But is not an equivalence relation because
NaN is not reflexive: NaN != NaN0 == '' and 0 == '0', but '' != '0'