Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active March 23, 2018 21:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfkaye/9992383da18207a2d576748b2e9ea3d8 to your computer and use it in GitHub Desktop.
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
// 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
// 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
// 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
@codezyc
Copy link

codezyc commented Sep 10, 2017

JavaScript is weird

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment