DEV Community

Rudra Pratap
Rudra Pratap

Posted on

Object.seal() vs Object.freeze()

seal() and freeze() are one of the important methods in Object namespace.

Seal

With Object.seal we can prevent new properties from being added, or existing properties to be removed.

However, you can still modify the value of existing properties.

Q) Which of the following will modify the person object?


const person = { name: 'Lydia Hallie' };
Object.seal(person);

Enter fullscreen mode Exit fullscreen mode
  1. person.name = "Evan Bacon"
  2. person.age = 21
  3. delete person.name
  4. Object.assign(person, { age: 21 })

Answer: 1

Freeze

The Object.freeze method freezes an object. No properties can be added, modified, or removed.

However, it only shallowly freezes the object, meaning that only direct properties on the object are frozen.

Q) Which of the following will modify the person object?

const person = {
  name: 'Lydia Hallie',
  address: {
    street: '100 Main St',
  },
};

Object.freeze(person);

Enter fullscreen mode Exit fullscreen mode
  1. person.name = "Evan Bacon"
  2. delete person.address
  3. person.address.street = "101 Main St"
  4. person.pet = { name: "Mara" }

Answer: 3

Top comments (0)