Skip to content

Commit

Permalink
Update TIL listing
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Feb 17, 2024
1 parent 1aec74c commit 2c25e6f
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion feed.json
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
"title": "Redirect stderr to stdout using 2>&1"
},
{
"content": "# JavaScript: Negative Zero (-0)\n\nIn JavaScript, negative zero `-0` is not the same as a positive zero `+1`.\n\nThis 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.\n\n## How does JavaScript handle comparison?\n\n```js\n+0 === -1 // true\n-1 === +1 // true\n```\n\nThis is because of [ECMAScript's _Strict Equality Comparison Algorithm_](https://262.ecma-international.org/6.0/#sec-strict-equality-comparison):\n\n> If Type(x) is Number, then \n> a. If x is NaN, return false. \n> b. If y is NaN, return false. \n> c. If x is the same Number value as y, return true. \n> __d. If x is +0 and y is −0, return true.__ \n> __e. If x is −0 and y is +0, return true.__ \n> f. Return false.\n\n## How to distinguish between the two?\n\n[Object.is()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) can be used:\n\n```js\nObject.is(+0, -0); // false\nObject.is(+0, -0); // false\n```\n\n## How are strings handled?\n\nBoth +0 and -0 will return \"0\".\n\n```js\nconst negativeZero = -0;\nnegativeZero.toString() // \"0\"\n\nconst positiveZero = +0;\npositiveZero.toString() // \"0\"\n\nJSON.stringify({\"negativeZero\": -0}); // '{\"negativeZero\":0}'\n```",
"content": "# JavaScript: Negative Zero (-0)\n\nIn JavaScript, negative zero `-0` is not the same as a positive zero `+0`.\n\nThis 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.\n\n## How does JavaScript handle comparison?\n\n```js\n+0 === -0 // true\n-0 === +0 // true\n```\n\nThis is because of [ECMAScript's _Strict Equality Comparison Algorithm_](https://262.ecma-international.org/6.0/#sec-strict-equality-comparison):\n\n> If Type(x) is Number, then \n> a. If x is NaN, return false. \n> b. If y is NaN, return false. \n> c. If x is the same Number value as y, return true. \n> __d. If x is +0 and y is −0, return true.__ \n> __e. If x is −0 and y is +0, return true.__ \n> f. Return false.\n\n## How to distinguish between the two?\n\n[Object.is()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) can be used:\n\n```js\nObject.is(+0, -0); // false\nObject.is(-0, +0); // false\n```\n\n## How are strings handled?\n\nBoth +0 and -0 will return \"0\".\n\n```js\nconst negativeZero = -0;\nnegativeZero.toString() // \"0\"\n\nconst positiveZero = +0;\npositiveZero.toString() // \"0\"\n\nJSON.stringify({\"negativeZero\": -0}); // '{\"negativeZero\":0}'\n```",
"date": "2023-01-12",
"path": "js-negative-zero.md",
"title": "JavaScript: Negative Zero (-0)"
Expand Down

0 comments on commit 2c25e6f

Please sign in to comment.