Skip to content

Commit

Permalink
Update js-negative-zero.md
Browse files Browse the repository at this point in the history
  • Loading branch information
petermekhaeil authored Feb 17, 2024
1 parent 0298bf9 commit 1aec74c
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions learnings/js-negative-zero.md
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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?
Expand Down

0 comments on commit 1aec74c

Please sign in to comment.