LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 3

sort() on numbers Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
console.log([5, 25, 100, 1].sort());
  • A [ 1, 5, 25, 100 ]
  • B [ 100, 25, 5, 1 ]
  • C [ 1, 100, 25, 5 ] Correct
  • D [ 5, 25, 100, 1 ]
Correct Answer: Option C

Without a comparator the numbers are compared as strings: "1" < "100" < "25" < "5".

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