Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Are JavaScript objects copied by reference?

Do you know what will be the output of this code?

JavaScript
const a = { n: 1 };
const b = a;
b.n = 2;
console.log(a.n);
Show answer and explanation

The answer is 2.

b = a copies a reference to the same object. There’s still only one object, so the change through b is visible through a.

Take the full quiz Next question →

More Basics questions

  1. Why do object keys 1 and "1" overwrite each other?
  2. What happens when you use an object as an object key?
  3. Why is {} === {} false in JavaScript?
  4. Does the spread operator make a deep copy?
  5. Is Object.freeze deep or shallow?

Related topics