Last active
March 23, 2018 21:41
-
-
Save dfkaye/9992383da18207a2d576748b2e9ea3d8 to your computer and use it in GitHub Desktop.
Surprising lte (<=) and gte (>=) operator results in JavaScript for null, object, and undefined
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 9 Sep 2017, following | |
// https://twitter.com/AbinavSeelan/status/903935309871812610 | |
// which explores the surprising `(null >= 0) == true` | |
// Not only is it surprising, it's bi-directional | |
console.log([ | |
// zero case | |
null <= 0, | |
null >= 0, | |
0 <= null, | |
0 >= null, | |
// empty string | |
null <= '', | |
null >= '', | |
'' >= null, | |
'' <= null, | |
// boolean | |
null <= false, | |
null >= false, | |
false <= null, | |
false >= null | |
].join(', ')); | |
// true, true, true, true, true, true, true, true, true, true, true, true, true, true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 9 Sep 2017 | |
// following | |
// https://twitter.com/bmeurer/status/906593961430712320 | |
// 2 different objects are different references, so... | |
console.log([ | |
{} < {}, | |
{} == {}, | |
{} === {}, | |
].join(', ')); | |
// false, false, false | |
// but... | |
console.log([ | |
{} <= {}, | |
{} >= {}, | |
].join(', ')); | |
// true, true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 9 Sep 2017 | |
// following | |
// https://twitter.com/bmeurer/status/906605033483108357 | |
// should be true because null *is* null... | |
console.log([ | |
null <= null, | |
null >= null | |
].join(', ')); | |
// true, true | |
// should be true because undefined *is* undefined, but... | |
console.log([ | |
void 0 <= void 0, | |
void 0 >= void 0, | |
undefined <= undefined, | |
undefined >= undefined, | |
].join(', ')); | |
// false, false, false, false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JavaScript is weird