Equality comparison operations
suggest changeJavaScript has four different equality comparison operations.
SameValue
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:
- Reflexivity:
Object.is(x, x)istrue, for any valuex - Symmetry:
Object.is(x, y)istrueif, and only if,Object.is(y, x)istrue, for any valuesxandy. - Transitivity: If
Object.is(x, y)andObject.is(y, z)aretrue, thenObject.is(x, z)is alsotrue, for any valuesx,yandz.
SameValueZero
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:
- Reflexivity:
.includes(x)istrue, for any valuex - Symmetry:
.includes(y)istrueif, and only if,[y].includes(x)istrue, for any valuesxandy. - Transitivity: If
.includes(y)and[y].includes(z)aretrue, then.includes(z)is alsotrue, for any valuesx,yandz.
Strict Equality Comparison
It behaves like SameValue, but
- Considers
+0and-0to be equal. - Considers
NaNdifferent than any value, including itself
You 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:
- Symmetry:
x === yistrueif, and only if, y === xistrue, for any valuesxandy`. - Transitivity: If
x === yandy === zaretrue, thenx === zis alsotrue, for any valuesx,yandz.
But is not an equivalence relation because
NaNis not reflexive:NaN !== NaN
Abstract Equality Comparison
If both operands belong to the same Type, it behaves like the Strict Equality Comparison.
Otherwise, it coerces them as follows:
undefinedandnullare considered to be equal- When comparing a number with a string, the string is coerced to a number
- When comparing a boolean with something else, the boolean is coerced to a number
- When comparing an object with a number, string or symbol, the object is coerced to a primitive
If 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:
- Symmetry:
x == yistrueif, and only if,y == xistrue, for any valuesxandy.
But is not an equivalence relation because
NaNis not reflexive:NaN != NaN- Transitivity does not hold, e.g.
0 == ''and0 == '0', but'' != '0'