or value, and whether or not that state can be changed.
An object or value is considered mutable
On the other hand, an object or value is considered immutable
// Mutable object (an array)
let mutableArray = [1, 2, 3];
console.log(mutableArray); // Output: [1, 2, 3]
console.log(mutableArray); // Output: [1, 2, 3, 4]
// Immutable object (a string)
let immutableString = "Hello";
console.log(immutableString); // Output: "Hello"
immutableString = immutableString.concat(", world!");
console.log(immutableString); // Output: "Hello, world!"
In the example above, we first declare a mutable object mutableArray, which is an array of numbers. We can modify the contents of this array by calling its push method, which adds a new element to the end of the array.
Notice that when we modify the mutable object mutableArray, the changes are reflected in the original array. However, when we "modify" the immutable object immutableString, we actually create a new string with the desired changes, and assign it to immutableString. The original string remains unchanged.