The Differences Between Object.freeze() vs Const in JavaScript

undefined or mostly null.
Search for a command to run...

undefined or mostly null.
No comments yet. Be the first to comment.
In this series, I will share articles related to best practices for building and deploying applications on the web with the JavaScript and Python programming languages.
As developers, it’s easy to assume that all users can see and use a keyboard, mouse or screen, you feel everyone can interact with your web page the same way you do. This can lead to a user experience that works well for some people but creates issue...
I wrote this article in December 2023 for an interview screening task and thought to share it today while clearing my drafts for some new content prep, just in case it's still helpful to someone out t

Hey! I’m super excited to announce that I have joined the Secretariat Team at the Digital Public Goods Alliance. In this role, I leverage my passion for open source and technical background to assess DPG applications, provide technical support and ad...

Creative designs have become more important than ever in the software ecosystem today with many industries and end-consumers having several use cases that require them to offer design editing solutions to either designers or end-consumers. Every busi...

With the rise of artificial intelligence (AI) and large language models (LLMs), it has become easier to solve different human problems than ever before. Even consumers with little to no technical expertise can benefit from AI. Humans can now automate...

Some years ago, GitHub introduced the new Profile README feature that allowed GitHub users to pin a markdown file on their profile using a special repository named after their GitHub username. Since then, developers have used this file as a quick por...

ES6 has brought several new features and methods into JavaScript since its release. These features have better improved our workflow and productivity as JavaScript developers. Amongst these new features are Object.freeze() method and const.
It is argued among a few developers especially newbies that these two feature works the same way, but they actually don’t. Object.freeze() and const work differently, and I'll show you how in this article.
const and Object.freeze() are totally different.
let. The only difference is, it defines a variable that cannot be reassigned. Variables declared by const are block-scoped and not function scoped like variables declared with var.Mutable objects have properties that can be changed. Immutable objects have no properties that can be changed after the object is created.
const user = 'Bolaji Ayodeji'
user = 'Joe Nash'
This would throw an Uncaught TypeError because we are trying to reassign the variable user, which was declared with the const keyword. This is not valid!

Originally, this would work with var or let but will not with const.
When working with Objects, using const only prevents reassignment and not immutability (the ability to prevent changes to the state and properties of an object after it's created).
Consider the code below. We have declared a variable using the const keyword and assigned an object named user to it.
const user = {
first_name: "bolaji",
last_name: "ayodeji",
email: "hi@bolajiayodeji.com",
net_worth: 2000,
};
user.last_name = 'Samson';
// this would work, user is mutable!
user.net_worth = 983265975975950;
// this would work too, user is mutable and getting rich :)!
console.log(user);
// user is mutated

Although we can’t reassign this variable called object, we can still mutate the object itself.
const user = {
user_name: 'bolajiayodeji'
}
// won't work

We definitely would want to have objects with properties that cannot be modified or deleted after creation. const cannot do this, and this is where Object.freeze() saves the day :).
To disable any changes to the object, we need Object.freeze().
const user = {
first_name: "bolaji",
last_name: "ayodeji",
email: "hi@bolajiayodeji.com",
net_worth: 2000,
};
Object.freeze(user);
user.last_name = 'Samson';
// this won't work, user is immutable!
user.net_worth = 983265975975950;
// this won't work too, user is still immutable and broke :(!
console.log(user);
// user is immutable

Object.freeze() is a bit shallow. An object with nested properties is not actually frozen when Object.freeze() is applied. You will need to apply Object.freeze() on nested objects to protect them recursively.
const user = {
first_name: 'bolaji',
last_name: 'ayodeji',
contact: {
email: 'hi@bolajiayodeji.com',
telephone: 08109445504,
}
}
Object.freeze(user);
user.last_name = 'Samson';
// this won't work, user is still immutable!
user.contact.telephone = 07054394926;
// this will work because the nested object is not frozen
console.log(user);

So Object.freeze() doesn't fully freeze an object when it has properties that are nested. To completely freeze objects and their nested properties, you can write your own library or use already created libraries like Deepfreeze or immutable-js.
I hope you've learned why const and Object.freeze() are not the same. const prevents reassignment and Object.freeze() prevents immutability.
Thanks for reading; cheers!