Skip to content
BENIBIX Learn

Immutability

Development🧭 Programming ParadigmsFunctional Programming

Also known as Immutable, Immutable Data

Data that cannot change once made. To change it, you make a new copy.

Think of it like

Think of it like a printed photo. To change it, you print a new one and keep the old.

Example

Jack freezes a point with x set to 1. A later line tries to set x to 5, and x is still 1.

Why it matters

Data that never changes cannot be broken by code far away.

Definition

Immutability means a value cannot change after it is made. To get a different value, you make a new one. The old one stays as it was. Python's text and tuples work this way, and JavaScript can freeze an object. Nobody can change data behind your back while you use it. Many tools use this to spot changes fast, by checking if a value is new.

Example code

javascript
const point = Object.freeze({ x: 1 });
point.x = 5;
console.log(point.x); // 1
The frozen object ignores the change.

Common mistake

Thinking const makes an object immutable. const stops you swapping the box, not changing what is in it.

Key takeaways

  1. 1
    Fixed once made. No edits in place.
  2. 2
    New copy instead. The old one stays.
  3. 3
    Safe to share. Nobody can change it.