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

Is Object.freeze deep or shallow?

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

JavaScript
const obj = Object.freeze({ a: 1, inner: { b: 1 } });
obj.a = 2;
obj.inner.b = 2;
console.log(obj.a, obj.inner.b);
Show answer and explanation

The answer is 1 2.

Object.freeze is shallow. The change to obj.a is ignored (in strict mode it throws), but inner is a separate object that isn’t frozen.

Take the full quiz Next question →

More Objects 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. Are JavaScript objects copied by reference?
  5. Does the spread operator make a deep copy?

Related topics