In JavaScript, negative zero -0 is not the same as a positive zero +0.
This is because numbers in JavaScript are represented using the IEEE 754 floating-point standard which requires zeros to have an associated sign. Floating point numbers include a sign bit (0 for positive, 1 for negative). In the case of +0, the sign bit is 0 while in the case of -0 the sign bit is 1.
If Type(x) is Number, then
a. If x is NaN, return false.
b. If y is NaN, return false.
c. If x is the same Number value as y, return true. d. If x is +0 and y is −0, return true. e. If x is −0 and y is +0, return true.
f. Return false.
JavaScript: Negative Zero (-0)
In JavaScript, negative zero
-0is not the same as a positive zero+0.This is because numbers in JavaScript are represented using the IEEE 754 floating-point standard which requires zeros to have an associated sign. Floating point numbers include a sign bit (0 for positive, 1 for negative). In the case of
+0, the sign bit is 0 while in the case of-0the sign bit is 1.How does JavaScript handle comparison?
This is because of ECMAScript's Strict Equality Comparison Algorithm:
How to distinguish between the two?
Object.is() can be used:
How are strings handled?
Both +0 and -0 will return "0".