Array Sort Default
Multiple Choice Medium
Question
What does [10,2,1].sort() return?
Hint
Default sort converts elements to strings.
Explanation
[10,2,1].sort() returns [1, 10, 2] because the default sort converts elements to strings and compares them lexicographically. "10" < "2" because "1" < "2" in string comparison. To sort numerically, pass a comparator: arr.sort((a, b) => a - b) which gives [1, 2, 10]. This is a very common JavaScript gotcha.
Related Resources
Related Lessons
- Arrays
Learn JS arrays.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.