How is a Javascript array compared?

Is there a specific standard for how JavaScript will be mapped, on the 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 

This is very unintuitive, and I can’t even figure out what logic is applied, and they look repeatable, so they don’t look based on the object identifier (ref), etc., is there any documentation for it?

+6
source share
2 answers

JavaScript matrices are converted to strings and then strings are compared. So.

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

Lines are compared lexicographically, so "5.0.0" is greater than "10.0.0".

+4
source

Something like this might help you,

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

Greetings :).

+1
source

Source: https://habr.com/ru/post/946651/


All Articles