diff --git a/learnings/js-negative-zero.md b/learnings/js-negative-zero.md index 4d41eae..df769c9 100644 --- a/learnings/js-negative-zero.md +++ b/learnings/js-negative-zero.md @@ -1,14 +1,14 @@ # JavaScript: Negative Zero (-0) -In JavaScript, negative zero `-0` is not the same as a positive zero `+1`. +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](http://en.wikipedia.org/wiki/IEEE_754) which requires [zeros to have an associated sign](http://en.wikipedia.org/wiki/Signed_zero). 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. ## How does JavaScript handle comparison? ```js -+0 === -1 // true --1 === +1 // true ++0 === -0 // true +-0 === +0 // true ``` This is because of [ECMAScript's _Strict Equality Comparison Algorithm_](https://262.ecma-international.org/6.0/#sec-strict-equality-comparison): @@ -27,7 +27,7 @@ This is because of [ECMAScript's _Strict Equality Comparison Algorithm_](https:/ ```js Object.is(+0, -0); // false -Object.is(+0, -0); // false +Object.is(-0, +0); // false ``` ## How are strings handled?