Object.freeze vs Object.seal

Object.freeze()

Object.freeze takes an object as an argument. Note that it modifies the object you pass as an argument. It does not copy the object and create a new one.

So what does it mean for the object?

  • You can’t add new properties to the object
  • You can’t modify the properties if they are not objects or arrays themselves. (More on this later)
  • You can’t delete properties from the object

With the description I just gave you, you should guess what the object will look like now:

The delete failed, modifying the name property failed, and adding the lastName property failed. But modifying the array and the nested sibling object worked.

Note: If you are not in strict mode, it will fail silently. In strict mode, you will get TypeErrors

What we have when we call Object.freeze is a shallow freeze. We only freeze what is directly attached to the object. Properties inside objects and arrays are not concerned.

To make the entire object and everything inside immutable, you have to call Object.freeze() on every element.

Let's use it on our first object:

Great! Now, our sibling object and our pets array can't be modified.

To know if an object is frozen, you can use Object.isFrozen():

Object.seal

Object.seal(), like the freeze method, takes a object as an argument. Object.seal is a softer version of Object.freeze.

  • You can’t remove or add elements to the object.
  • You can modify existing properties.

Pretty straightforward huh? Let’s try to modify this object now:

Maybe you already guessed what should happen 😉

Notice that just like Object.freeze, Object.seal will fail silently in non-strict mode and throw a TypeError in strict mode.

We also have a method to know if an object is sealed. Quite explicit, we can call Object.isSealed to know that:

What about const?

You might be tempted to look at Object.seal and Object.freeze and compare them to const. Remember that they are different concepts. Object.freeze and Object.seal apply on the values of an object, and const applies on the binding. Object.freeze makes an object immutable. const creates immutable binding. Once you assign a value to a variable, you can’t assign a new value to that binding.

What about prototypes?

One last thing I need to mention: prototypes. When it comes to Object.freeze and Object.seal, know that you also can’t change their prototypes once they are frozen or sealed.

setPrototypeOf is used to change the prototype of an object. When the object is sealed or frozen, you will not be able to do that. As always, in non-strict mode, it will fail silently. In strict mode, you will see a TypeError: Object is not extensible.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhay Jain

Abhay Jain

Developer with 3 yrs of industrial experience in developing scalable web applications.