Question 17
Back to Exercise
What does the following JavaScript code print (console output, as in Node.js)?
const a = [[1], [2]];
const b = [...a];
b[0].push(9);
b[1] = [0];
console.log(a);
- A [ [ 1 ], [ 2 ] ]
- B [ [ 1, 9 ], [ 0 ] ]
- C [ [ 1, 9 ], [ 2 ] ] Correct
- D [ [ 1 ], [ 0 ] ]
Correct Answer: Option C
The spread copy is a new outer array holding the same inner arrays. Pushing into b[0] changes the shared inner array; replacing b[1] changes only b.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!