How to use Object.freeze() in JavaScript?

How to use Object.freeze() in JavaScript?

Objects and arrays in JavaScript are mutable. This means that we can change an object (or an array) whenever we want. But if we don't want that to happen for any reason and want to make the object immutable.


We can do this using the freeze() method.


An object that you freeze with the freeze() method can no longer be changed; freezing an object with freeze prevents adding new properties, removing existing properties, changing the enumerability, configurability, or writability of existing properties, and changing the values of existing properties. Additionally, freezing an object also prevents the prototype from being modified.


I can add an example code regarding the usage of this method;
const obj = {
    name: "Jack"
};

Object.freeze(obj);

obj.name = "Kelly";
// Throws an error in strict mode

console.log(obj.name);
// expected output: "Jack"



Looking for authors and journalists!

Looking for authors and journalists!

If you are a journalist, who has desire to dig deep in topics related to businesses and sectors, we want to work with you!

Erkan Sivas

PlusClouds Enthusiast
Sales

Product information

#There is no text provided for translation. Please provide the text you would like to be translated into English.
Auto Quill
AutoQuill writes and posts affiliate marketing content for you.

Easiest Money You Will Ever Make

With the AutoQuill tool that PlusClouds has started to offer to its affiliate partners, content creators will be able to sell with a single click. Let us show you how you can get your share of this revolution.