Immutable
An immutable value is one whose content cannot be changed without creating an entirely new value, in comparison with mutable values.
An **immutable** value is one whose content cannot be changed without creating an entirely new value, in comparison with mutable values.
In JavaScript, primitive values are immutable — once a primitive value is created, it cannot be changed, although the variable that holds it may be reassigned another value.
By contrast, objects and arrays are mutable by default — their properties and elements can be changed without reassigning a new value.
It can be beneficial to use immutable objects for several reasons:
- To improve performance (no planning for the object's future changes)
- To reduce memory use (make object references instead of cloning the whole object)
- Thread-safety (multiple threads can reference the same object without interfering with one other)
- Lower developer mental burden (the object's state won't change and its behavior is always consistent)
Note that you can easily prove mutability: an object is mutable as long as it offers one way to change its properties. On the other hand, _immutability_ is hard to prove if there are no language semantics to secure it — it is a matter of developer contract. For example, Object.freeze() is a language-level method to make an object immutable in JavaScript.
## See also
- [Immutable object](https://en.wikipedia.org/wiki/Immutable_object) on Wikipedia
- Related glossary terms:
- Mutable
Cite this
"Immutable". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Glossary/Immutable JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy