LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 14

fill with an array Answer, explanation & discussion

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

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

main.js
const grid = new Array(2).fill([]);
grid[0].push(1);
console.log(grid);
  • A [ [ 1 ], [] ]
  • B [ [ 1 ], [ 1 ] ] Correct
  • C [ 1, [] ]
  • D [ [], [] ]
Correct Answer: Option B

Both slots point to the same inner array, so the push shows up in both rows.

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