Skip to main content

Immutability

In Turkish
Değişmezlik
Pronunciation
ih-myoo-tuh-BIL-ih-tee
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/immutability

In short

Immutability means a value cannot be changed after it is created, so every update produces a new value instead of modifying the original in place.

What is immutability?

Immutability is the property of data that cannot be changed once it has been created. If you want a different value, you create a new one, and the original stays exactly as it was. Some values are immutable in most languages: strings in Java, Python and JavaScript, for example, can't be edited in place, so every "change" returns a new string.

Languages support immutability in different ways. Some make it the default, as in Haskell, Elixir, Erlang and Clojure, where data structures are never modified. Others offer it as an option, such as final in Java, val in Kotlin, readonly in TypeScript, tuples and frozen dataclasses in Python, and Object.freeze in JavaScript. To keep copying cheap, functional languages use persistent data structures, which share the unchanged parts between the old version and the new one instead of duplicating everything.

A printed receipt is a good analogy: once it's printed, nobody erases the total; if something changes, a new receipt is issued and the old one remains a reliable record. Immutable data is easier to reason about because no other part of the program can change it behind your back, and it can be shared between threads without locks. UI libraries such as React rely on it to detect changes quickly, since a new object means something changed.

Immutability is often confused with constants. In JavaScript, const only stops a variable from being reassigned; the object it refers to can still be modified, so const user = {}; user.name = "Ada"; works fine. A truly immutable value cannot be changed through any variable at all.

Key takeaways

  • An immutable value never changes after it is created; updates produce new values.
  • Immutable data can be shared safely between functions and threads.
  • Functional languages make immutability the default and use persistent data structures to keep copies cheap.
  • A constant binding, such as JavaScript's const, is not the same as an immutable value.

Example

Updating immutable data by copyingjavascript
const user = Object.freeze({ name: "Ada", role: "dev" });

// user.role = "admin"; // ignored (throws in strict mode)

// Instead of changing user, create an updated copy
const promoted = { ...user, role: "admin" };

console.log(user.role);     // "dev"   (original untouched)
console.log(promoted.role); // "admin"

Readers ask

Is `const` the same as immutable in JavaScript?

No. const prevents the variable from being reassigned, but the object or array it refers to can still be changed. To stop changes to the object itself, use Object.freeze, which is shallow, or follow a style that always creates new objects instead of mutating them.

Is immutable data slow?

Copying a large structure on every change would be slow, but most immutable designs avoid full copies. Persistent data structures share unchanged parts between versions, so the overhead is usually small compared with the bugs it prevents.

Why does immutability help with concurrency?

Race conditions happen when threads change the same data at the same time. If data can never change, threads can read it freely without locks, because there is nothing to conflict over.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings