Skip to content
Justice Tech School

Immutable Updates

Use readonly arrays and spread to update without mutating.

Example
const a: readonly number[] = [1, 2];
const b = [...a, 3];
const o = { x: 1 }; const o2 = { ...o, x: 2 };
console.log(a, b, o, o2);

Result

[ 1, 2 ] [ 1, 2, 3 ] {
  x: 1,
} {
  x: 2,
}

Exercise

Readonly array.

 number[]

Quiz Test

What does readonly number[] stop?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.