0

if([5,3,4,6,7,8,9,1,2].sort() == [1,2,3,4,5,6,7,8,9]){console.log("It works")}
console.log([1,2,3,4,5,6,7,8,9] == [5,3,4,6,7,8,9,1,2].sort());
Why does it return false ? Aren't they the same ? }
  • Because you are comparing two different objects and this way their reference is compared, which is not the same. See here: https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – Krisztián Balla Mar 15 '20 at 10:31
  • Aside from the comparing arrays thing, it's probably even easier to *check* if an array is sorted, as opposed to comparing it against an already sorted array. – VLAZ Mar 15 '20 at 10:37

0 Answers0