6

Is there a standard defined how would JavaScript be compared, on Chrome console I get this

[10,0,0] > [1,0,0]
true
[10,0,0] > [5,0,0]
false
[5, 0, 0, 0] < [10, 0, 0, 0] //repeatable
false

[10,0,0,0] > [9,0,0,0]
false
[11,0,0,0] > [10,0,0,0]
true

Which is highly unintutive, and I can't even make sense what logic is being applied, and they look repeatable so doesn't look based on object id(ref) etc, so is there any documentation for it?

Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219

2 Answers2

4

JavaScript arrays are converted to strings and the strings are then compared. So.

[10,0,0].toString() => "10,0,0"
[5,0,0].toString() => "5,0,0"

Strings are compared lexicographically, so "5,0,0" is bigger than "10,0,0".

Stasik
  • 2,568
  • 1
  • 25
  • 44
1

Something like this may help you,

JSON.stringify([2,2,2]) === JSON.stringify([2,2,2]); //true

Cheers :).