🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Functions, Arrays and Objects: all 10 questions

Quiz: Functions, Arrays and Objects · Question 6 of 10

What does this print? (code)

What does this print?

const nums = [3, 1, 2];
const sorted = [...nums].sort((a, b) => a - b);
console.log(nums, sorted);
  1. A.[ 1, 2, 3 ] [ 1, 2, 3 ]
  2. B.[ 3, 1, 2 ] [ 1, 2, 3 ]
  3. C.[ 3, 1, 2 ] [ 3, 1, 2 ]
  4. D.[ 1, 2, 3 ] [ 3, 1, 2 ]
Answer & explanation

Correct answer: [ 3, 1, 2 ] [ 1, 2, 3 ]

Copying with spread first keeps the original array unchanged, because sort changes the array it is called on.

Can you get the rest right without peeking?

Take the timed Quiz: Functions, Arrays and Objects quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →