Why would I need to freeze an object in JavaScript? Why would I need to freeze an object in JavaScript? javascript javascript

Why would I need to freeze an object in JavaScript?


The Object.freeze function does the following:

  • Makes the object non-extensible, so that new properties cannot be added to it.
  • Sets the configurable attribute to false for all properties of the object. When - configurable is false, the property attributes cannot be changed and the property cannot be deleted.
  • Sets the writable attribute to false for all data properties of the object. When writable is false, the data property value cannot be changed.

That's the what part, but why would anyone do this?

Well, in the object-oriented paradigm, the notion exists that an existing API contains certain elements that are not intended to be extended, modified, or re-used outside of their current context. The final keyword in various languages is the most suitable analogy of this. Even in languages that are not compiled and therefore easily modified, it still exists, i.e. PHP, and in this case, JavaScript.


You can use this when you have an object representing a logically immutable data structure, especially if:

  • Changing the properties of the object or altering its "duck type" could lead to bad behavior elsewhere in your application
  • The object is similar to a mutable type or otherwise looks mutable, and you want programmers to be warned on attempting to change it rather than obtain undefined behavior.

As an API author, this may be exactly the behavior you want. For example, you may have an internally cached structure that represents a canonical server response that you provide to the user of your API by reference but still use internally for a variety of purposes. Your users can reference this structure, but altering it may result in your API having undefined behavior. In this case, you want an exception to be thrown if your users attempt to modify it.


In my nodejs server environment, I use freeze for the same reason I use 'use strict'. If I have an object that I do not want being extended or modified, I will freeze it. If something attempts to extend or modify my frozen object, I WANT my app to throw an error.

To me this relates to consistent, quality, more secure code.

Also, Chrome is showing significant performance increases working with frozen objects.

Edit:In my most recent project, I'm sending/receiving encrypted data between a government entity. There are a lot of configuration values. I'm using frozen object(s) for these values. Modification of these values could have serious, adverse side effects. Additionally, as I linked previously, Chrome is showing performance advantages with frozen objects, I assume nodejs does as well.

For simplicity, an example would be:

var US_COIN_VALUE = {        QUARTER: 25,        DIME: 10,        NICKEL: 5,        PENNY: 1    };return Object.freeze( US_COIN_VALUE );

There is no reason to modify the values in this example. And enjoy the benefits of speed optimizations.