DEV Community

Cover image for JavaScript Quiz Question #3: Deep Object Mutability
Nick Scialli (he/him)
Nick Scialli (he/him)

Posted on

JavaScript Quiz Question #3: Deep Object Mutability

Consider the following object representing a user, Joe, and his dog, Buttercup. We use Object.freeze to preserve our object and then attempt to mutate Buttercup's name. What gets logged?

const user = {
  name: 'Joe',
  age: 25,
  pet: {
    type: 'dog',
    name: 'Buttercup'
  }
};

Object.freeze(user);

user.pet.name = 'Daffodil';

console.log(user.pet.name);
Enter fullscreen mode Exit fullscreen mode

A) Daffodil
B) Buttercup
C) An error is thrown

Put your answer in the comments!

Top comments (3)

Collapse
 
wierdorohit123 profile image
rohit raut

Daffodil, since Object.freeze cannot prevent mutation of nested objects.

Collapse
 
upieez profile image
Samuel Huang

I never knew that. I would have assumed that it would still be buttercup. So if the above example say the pet name wasn't nested, then what would be the answer?

Collapse
 
araw830 profile image
Atul Rawat

Daffodil