Skip to content

Instantly share code, notes, and snippets.

@founddrama
Created February 7, 2011 23:50
Show Gist options
  • Save founddrama/815552 to your computer and use it in GitHub Desktop.
Save founddrama/815552 to your computer and use it in GitHub Desktop.
You may encounter some strange things when comparing single-item Arrays with other things in JavaScript.
[1] == [1];
// false
[1] == 1;
// true
[1] === 1;
// false
[1] == '1';
// true
[1, 1] == 1;
// false (of course?)
[1, 1] == '1,1';
// true
// and then it gets weird:
[true] == true;
// false; but compare with:
true == true;
// true (sanity check...)
[true] == 'true';
// true (there's that valueOf/toString again)
// now...
[1] == true;
// true
['1'] == true;
// true
[''] == false;
// true
[0] == false;
// true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment