Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New post on array comparison

Signed-off-by: DTrejo <david.daniel.trejo@gmail.com>
  • Loading branch information...
commit a9b233cdb5e46da3e492cc83f0f73a1c351a486f 1 parent 24cb13f
@pwnall pwnall authored DTrejo committed
Showing with 26 additions and 0 deletions.
  1. +26 −0 posts/2013-01-28-array-comparison.md
View
26 posts/2013-01-28-array-comparison.md
@@ -0,0 +1,26 @@
+Did you know that JavaScript can compare arrays using lexicographical ordering?
+
+```
+ [1, 2, 4] < [1, 2, 5] // true
+ [1, 3, 4] < [1, 2, 5] // false
+```
+
+Just don't expect trichotomy to hold.
+
+```
+ [1, 2, 3] === [1, 2, 3] // false
+ [1, 2, 3] < [1, 2, 3] // false
+ [1, 2, 3] == [1, 2, 3] // false
+ [1, 2, 3] > [1, 2, 3] // false
+```
+
+Oh, and just in case you're wondering, it knows it's messing with you.
+
+```
+ [1, 2, 3] <= [1, 2, 3] // true
+ [1, 2, 3] >= [1, 2, 3] // true
+```
+
+— [@pwnall][1]
+
+[1]:https://twitter.com/pwnall

1 comment on commit a9b233c

@benben77

I think what really happens is sth else: when using > and <, because array has not implemented with them, so will call toString to compare two array, and when comparing strings is one char by one char, which explain the result of < and >.
and when using ==, js will just check if they point to the same object, if u use var arr=[0,1,2], arr === arr will return true.
a >= b is sth like: (a != b) && (a > b), which will return true when a != b

Please sign in to comment.
Something went wrong with that request. Please try again.