MediumArray Methods💻 Output Question

Spread copy before sort preserves original

💡

Hint

sort() mutates in place. Always spread first to avoid mutating the source.

What does this output?

const original = [3, 1, 2];
const sorted = [...original].sort();
console.log(original.join(','));
console.log(sorted.join(','));

Correct Output

3,1,2
1,2,3

Why this output?

Explanation: [...original] creates an independent copy. sort() mutates sorted, not original.

Key Insight: sort() mutates in place. Always spread first to avoid mutating the source.

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