[1, 2, 3] === [1, 2, 3] // false
[1, 2, 3] == [1, 2, 3] // false
Arrays are objects in JavaScript and therefore compared by reference (regardless whether with strict or typecasting equality). Two distinct (even if similar) literals always evaluate to distinct objects, which will never equal. To cite the spec,
[If both x
and y
are of type Object
, then] return true
if x
and y
refer to the same object. Otherwise, return false
.
[1, 2, 4] < [1, 2, 5] // true
…
JavaScript's comparison operators do always cast their arguments to primitive values (and then to strings or numbers if needed). On objects, first .valueOf()
then .toString()
is tried. Arrays will become stringified, and for your simple example "1,2,4"
indeed is smaller than "1,2,5"
. It would not work for complex objects, strings that contain commata or even for [12]
vs [2]
.