Algorithms and Data Structures · Junior · Question 34 of 75

What does this code return?

[10, 1, 5, 100].sort();

Explanation

Without a comparator sort converts elements to strings and compares them as text, so "10" and "100" come before "5". For numbers pass a comparator: sort((a, b) => a - b).

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 34

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz