LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 7

Changing a spread copy Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

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

main.js
const a = { n: 1, d: { x: 1 } };
const b = { ...a };
b.n = 2;
b.d.x = 2;
console.log(a.n, a.d.x);
  • A 2 2
  • B 1 1
  • C 2 1
  • D 1 2 Correct
Correct Answer: Option D

n is copied by value into b; d is a reference to the same nested object, so b.d.x = 2 is visible through a.

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