Object.seal() vs Object.freeze()

merudra754

Rudra Pratap

Posted on May 8, 2023

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

đź’– đź’Ş đź™… đźš©
merudra754
Rudra Pratap

Posted on May 8, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related