MediumArray Methods💻 Output Question

Default sort is lexicographic — not numeric

💡

Hint

Default Array.sort() is lexicographic. Always pass (a,b) => a-b for numeric sorting.

What does this output?

const nums = [10, 9, 2, 1, 100];
console.log([...nums].sort().join(','));
console.log([...nums].sort((a, b) => a - b).join(','));

Correct Output

1,10,100,2,9
1,2,9,10,100

Why this output?

Explanation: Default sort converts to strings: "10" < "2" because "1"<"2". With comparator (a-b): numeric sort works.

Key Insight: Default Array.sort() is lexicographic. Always pass (a,b) => a-b for numeric sorting.

More Array Methods Output Questions

Easymap transforms each elementMediumfilter then map chainMediumreduce accumulates to a single valueMediumfind returns first match; filter returns all

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz