The following puzzle has been
posted on Reddit by davvblack: What are the values of
x and
y, given the following interaction?
> x === y
true
> 1/x > 1/y
true
Show answer
x is +0,
y is -0.
+0 and -0 are the only two values that are not the same, but are still considered strictly equal [1]. They can only be distinguished indirectly, by applying a mathematical operation to them that produces distinguishable results. Dividing 1 by zero is one of several operations that does so:
> 1/-0
-Infinity
> 1/+0
Infinity
The infinities are distinguishable:
> -Infinity === Infinity
false
Further reading:
- “Stricter equality in JavaScript” explores more consequences of strict equality’s special treatment of +0, -0 and NaN.
- “JavaScript’s two zeros” provides more information on the two zeros. For example, other ways than the above for distinguishing them.
Comments
Post a Comment