JavaScript · Middle · Question 34 of 75
What does [10, 1, 2].sort() return?
- ✓Correct answer:
[1, 10, 2] - 2
[1, 2, 10] - 3
[10, 2, 1]
Explanation
Without a compare function, sort compares elements as strings, and '10' < '2'. For numbers you need sort((a, b) => a - b). Also, sort mutates the array; the non-mutating version is toSorted().