docs.rodeo

MDN Web Docs mirror

Immutable

{{GlossarySidebar}} 

An immutable value is one whose content cannot be changed without creating an entirely new value, in comparison with {{Glossary("mutable", "mutable values")}} .

In {{glossary("JavaScript")}} , {{Glossary("primitive", "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, {{Glossary("Object","objects")}}  and {{Glossary("Array","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:

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, {{jsxref("Object.freeze()")}}  is a language-level method to make an object immutable in JavaScript.

See also

In this article

View on MDN