Immutability in JavaScript

Immutability, defined in a single line, means that once that value is assigned, then it can never be changed

var string1 = "I am an immutable";var string2 = string1.slice(4, 8);

string1.slice does not change the value of string1. In fact, no string methods change the string they operate on, they all return new strings. The reason is that strings are immutable—they cannot change.

Strings are not the only immutable entity in JavaScript. Numbers, too, are immutable.

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.