Skip to content

mutability

Mutability is the ability of a value to change in place after it’s created, so the same value can hold different contents over time.

A value that can’t change is immutable, and any change to it produces a new value instead. Every primitive is immutable. A method such as .toUpperCase() returns a new string and leaves the original as it was.

Objects, including arrays and functions, are mutable by default. Code can add, change, or delete their properties, and every reference to the object sees the change. Mutability is a property of the value itself. A const binding can’t be reassigned, but the object it holds stays mutable.

Many array methods mutate their array, such as .push(), .sort(), and .reverse(). ES2023 added copying versions, such as .toSorted() and .toReversed(), that return a new array and leave the original untouched. The two kinds of sort differ in which array ends up reordered:

Calling .toSorted() on finishOrder returns a new sorted array, while .sort() reorders the same array that finishOrder holds.
Mutating Sort vs Copying Sort

Object.freeze() makes an object immutable. A frozen object can’t gain, lose, or change properties. The freeze is shallow, so objects nested inside it stay mutable. Writes to a frozen object fail silently in sloppy mode and throw a TypeError in strict mode.

Example

Say a game shows a ranked leaderboard but also keeps the scores in the order players finished:

Language: JavaScript
> const finishOrder = [42, 97, 65];
undefined

> finishOrder.toSorted((a, b) => b - a)
[ 97, 65, 42 ]
> finishOrder
[ 42, 97, 65 ]

> finishOrder.sort((a, b) => b - a);
[ 97, 65, 42 ]
> finishOrder
[ 97, 65, 42 ]

The .toSorted() call returned a sorted copy, so the finish order survived. The .sort() call mutated the array itself, so the original order is gone, even though finishOrder is a const binding.

Say an app freezes its settings at startup so that no module can change them:

Language: JavaScript
> const settings = Object.freeze({ theme: "dark", limits: { uploads: 5 } });
undefined

> settings.theme = "light";
'light'
> settings.theme
'dark'

> settings.limits.uploads = 50;
50
> settings
{ theme: 'dark', limits: { uploads: 50 } }

The REPL runs in sloppy mode, so the write to theme failed without an error and the frozen value stayed "dark". The nested limits object wasn’t frozen, so its uploads limit still changed.

A developer points at a tower where a robotic gripper reorders colored rows, beside a copier duplicating cards and a row frozen in ice.

Course

Copying, Freezing, and Sorting JavaScript Arrays for Pythonistas

Port a five-a-side league's Python table script to JavaScript, and learn how to copy, freeze, and sort arrays and records without changing data you meant to keep.

intermediate js py

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 17, 2026