JavaScript · Middle · Question 34 of 75

What does [10, 1, 2].sort() return?

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().

Further reading

Problem with a question

JavaScript · Middle · question 34

Test yourself

Take the JavaScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz