Strict equality using ===
Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal. Otherwise, if the values have the same type and are not numbers, they’re considered equal if they have the same value. Finally, if both values are numbers, they’re considered equal if they’re both not NaN and are the same value, or if one is +0 and one is -0.
Strict equality is almost always the correct comparison operation to use. For all values except numbers, it uses the obvious semantics: a value is only equal to itself. For numbers it uses slightly different semantics to gloss over two different edge cases. The first is that floating point zero is either positively or negatively signed. This is useful in representing certain mathematical solutions, but as most situations don’t care about the difference between +0 and -0, strict equality treats them as the same value. The second is that floating point includes the concept of a not-a-number value, NaN, to represent the solution to certain ill-defined mathematical problems: negative infinity added to positive infinity, for example. Strict equality treats NaN as unequal to every other value – including itself. (The only case in which (x !== x) is true is when x is NaN.)
Loose equality using ==
Loose equality compares two values for equality, after converting both values to a common type. After conversions (one or both sides may undergo conversions), the final equality comparison is performed exactly as === performs it. Loose equality is symmetric: A == B always has identical semantics to B == A for any values of A and B (except for the order of applied conversions).
In the above table, ToNumber(A) attempts to convert its argument to a number before comparison. Its behavior is equivalent to +A (the unary + operator). ToPrimitive(A) attempts to convert its object argument to a primitive value, by attempting to invoke varying sequences of A.toString and A.valueOf methods on A.
Traditionally, and according to ECMAScript, all objects are loosely unequal to undefined and null. But most browsers permit a very narrow class of objects (specifically, the document.all object for any page), in some contexts, to act as if they emulate the value undefined. Loose equality is one such context: null == A and undefined == A evaluate to true if, and only if, A is an object that emulates undefined. In all other cases an object is never loosely equal to undefined or null.
Same-value equality
Same-value equality addresses a final use case: determining whether two values are functionally identical in all contexts. (This use case demonstrates an instance of the Liskov substitution principle.) One instance occurs when an attempt is made to mutate an immutable property:
Object.defineProperty will throw an exception when attempting to change an immutable property would actually change it, but it does nothing if no actual change is requested. If v is -0, no change has been requested, and no error will be thrown. But if v is +0, Number.NEGATIVE_ZERO would no longer have its immutable value. Internally, when an immutable property is redefined, the newly-specified value is compared against the current value using same-value equality.
Same-value equality is provided by the Object.is method.
Same-value-zero equality
Similar to same-value equality, but considered +0 and -0 equal.
Sameness Comparisons
x | y | == | === | Object.is | SameValueZero |
---|---|---|---|---|---|
undefined | undefined | true | true | true | true |
null | null | true | true | true | true |
true | true | true | true | true | true |
false | false | true | true | true | true |
‘foo’ | ‘foo’ | true | true | true | true |
0 | 0 | true | true | true | true |
+0 | -0 | true | true | false | true |
+0 | 0 | true | true | true | true |
-0 | 0 | true | true | false | true |
0 | false | true | false | false | false |
“” | false | true | false | false | false |
“” | 0 | true | false | false | false |
‘0’ | 0 | true | false | false | false |
‘17’ | 17 | true | false | false | false |
[1, 2] | ‘1,2’ | true | false | false | false |
new String(‘foo’) | ‘foo’ | true | false | false | false |
null | undefined | true | false | false | false |
null | false | false | false | false | false |
undefined | false | false | false | false | false |
{ foo: ‘bar’ } | { foo: ‘bar’ } | false | false | false | false |
new String(‘foo’) | new String(‘foo’) | false | false | false | false |
0 | null | false | false | false | false |
0 | NaN | false | false | false | false |
‘foo’ | NaN | false | false | false | false |
NaN | NaN | false | false | true | true |