DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Differences between Object.freeze( ) and Object.seal( ) in Javascript
Maria Antonella 🦋
Maria Antonella 🦋

Posted on

Differences between Object.freeze( ) and Object.seal( ) in Javascript

They are functions that may appear to do the same action but have significant differences. And it was difficult for me to learn it. I don't know why but they always confused me.

👉 Object.freeze(): It prevents you from adding new properties, removing existing properties and modifying them.

👉 Object.seal(): You can modify existing properties but you can't delete or add new ones.

CRUD Operations

If we differentiate them through the CRUD operations: Create - Read - Update - Delete, we obtain the following comparison.

                  Create     Read    Update     Delete
Object.freeze()    ❌        ☑️      ❌         ❌
Object.seal()      ❌        ☑️      ☑️         ❌

Enter fullscreen mode Exit fullscreen mode

The wonderful world of Javascript. That's all for today :)

Top comments (5)

Collapse
 
lukeshiru profile image
Luke Shiru

Kinda related, luckily we will soon get Record and Tuple, two types that are "frozen by default". The proposal is in stage 2, but you can test it already in Babel. The main difference is that it has deep equality, so:

// (-ω-、) Different ...
Object.freeze({ foo: "bar" }) !== Object.freeze({ foo: "bar" });

// ヽ(o^▽^o)ノ Equal!
#{ foo: "bar" } === #{ foo: "bar" };
Enter fullscreen mode Exit fullscreen mode

Cheers!

Collapse
 
itays123 profile image
Itay Schechner

Whoa! That's fascinating!

Collapse
 
jfbrennan profile image
Jordan Brennan

They're not deep either. I thought freeze()ing meant nothing could change, but that's only true for the top level of properties. Property values that are objects do not get frozen.

Collapse
 
clamstew profile image
Clay Stewart

Sounds like a job for deep-freeze npmjs.com/package/deep-freeze

Collapse
 
clementdunstan profile image
clement dunstan siaffu

nice

Search No More

Join DEV and MongoDB and build an application with full-text search capabilities using MongoDB Atlas and Atlas Search for the DEV x MongoDB Atlas Hackathon 2022.

→ Join the Hackathon