Immutability is the property of data that cannot be modified once it has been created. To "change" an immutable value, a program creates a new value that includes the change and leaves the original untouched.
Why it matters
Immutable data removes a whole class of bugs in which one part of a program silently alters data that another part is using. It makes code easier to reason about, simplifies caching and undo, and is safe to share between threads without locks, which is why functional languages and libraries such as React and Redux favor it. Strings are immutable in Java, Python and JavaScript. The trade-off is that naive copying can cost time and memory, so efficient immutable structures share unchanged parts between versions.
Example
const site = Object.freeze({ host: "example.com", up: true });
site.up = false; // ignored (error in strict mode)
const down = { ...site, up: false }; // new object, original unchanged
Common mistakes
- Assuming
constmakes an object immutable; it only prevents reassigning the variable, not changing the contents. - Freezing only the top level while nested objects stay mutable.
- Copying huge structures on every small change.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.