DEV Community

Rudra Pratap
Rudra Pratap

Posted on

5

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay