Reading through the ECMAScript 5.1 specification, +0 and -0 are distinguished.
Why then does +0 === -0 evaluate to true?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
JavaScript uses IEEE 754 standard to represent numbers. From Wikipedia:
The article contains further information about the different representations.
So this is the reason why, technically, both zeros have to be distinguished.
This behaviour is explicitly defined in section 11.9.6, the Strict Equality Comparison Algorithm (emphasis partly mine):
(The same holds for
+0 == -0btw.)It seems logically to treat
+0and-0as equal. Otherwise we would have to take this into account in our code and I, personally, don’t want to do that 😉Note:
ES2015 introduces a new comparison method,
Object.is.Object.isexplicitly distinguishes between-0and+0: