Immutability
- In Turkish
- Değişmezlik
- Pronunciation
- ih-myoo-tuh-BIL-ih-tee
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
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
- Functional ProgrammingProgramming Fundamentals, p. 22Functional programming is a style of building software from pure functions that avoid changing shared data, making code more predictable and easier to test.
- Pure FunctionProgramming Fundamentals, p. 47A pure function always returns the same output for the same input and has no side effects, meaning it does not change anything outside itself.
- ConcurrencyProgramming Fundamentals, p. 12Concurrency is a program's ability to make progress on several tasks in overlapping time periods, such as serving many users at once rather than one at a time.
- Race ConditionOperating Systems, p. 24A race condition is a bug where a program's result depends on the unpredictable timing of threads, processes, or requests that use shared data at the same time.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- ClojureProgramming Languages, p. 6Clojure is a modern, dynamic dialect of Lisp that runs on the JVM, emphasizing immutable data, functional programming and interactive, REPL-driven development.
Spotted a mistake or something missing on this page?Suggest an edit