LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 17

Spread copy of nested arrays Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 17 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links