LearnFrenzy
Sign in Take a free test

Arrays · JavaScript Arrays MCQ · Question 4

How does sort() compare by default? Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

Without a compare function, how does [25, 100, 8].sort() order the numbers?

  • A Numerically ascending: [8, 25, 100]
  • B Numerically descending: [100, 25, 8] because of size
  • C It leaves them unchanged
  • D It converts them to strings and compares them character by character: [100, 25, 8] Correct
Correct Answer: Option D

"100" < "25" < "8" as strings, because "1" < "2" < "8". The descending-looking result is a coincidence of string order. Use sort((a, b) => a - b).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links