← 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);
- A.[ 1, 2, 3 ] [ 1, 2, 3 ]
- B.[ 3, 1, 2 ] [ 1, 2, 3 ]
- C.[ 3, 1, 2 ] [ 3, 1, 2 ]
- 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 →