It seems rather counter-intuitive for arrays declared as const to have mutable elements. What is the reasoning behind this design decision? More over if one should need a truly immutable array in JS how would that be done?
-
3`const` just makes the binding immutable, not the object assigned to it. – VLAZ Mar 08 '22 at 08:14
-
1It wasn't so much "a decision" and more of a consequence of how arrays already work. – evolutionxbox Mar 08 '22 at 08:15
-
To make an object (which an array is) immutable, use `Object.freeze`: `const arr = [0, 1]; Object.freeze(arr); try { arr.push(2); } catch (error) { console.error(error); }` – connexo Mar 08 '22 at 08:45
3 Answers
Const is not meant to make your element immutable! it only ensures that your element stays the same so you cannot assign to it a different element!

- 54
- 6
You can freeze the array with Object.freeze
. This would make it so the elements cannot be changed.
const arr = [1, 2, 3];
arr.push(4); // arr = [1, 2, 3, 4]
const frozenArr = Object.freeze(arr);
frozenArr.push(5); // Error: Cannot add property
frozenArr.pop(); // Error: Cannot delete property
Previous answer as it was, for reference
A const
array only prevents further assignments but you are allowed to change the contents of it.
The best practice for immutable arrays is to make copies, i.e. use Array.slice()
with no arguments or Array.from()
.
Example:
const originalArray = [1, 2, 3];
let firstNewArray = Array.from(originalArray);
firstNewArray.push(4);
let secondNewArray = originalArray.slice();
secondNewArray.push(5);
console.log(originalArray); // Output -> [1, 2, 3]
console.log(firstNewArray); // Output -> [1, 2, 3, 4]
console.log(secondNewArray); // Output -> [1, 2, 3, 5]
originalArray.push(9); // This is allowed
console.log(originalArray); // Output -> [1, 2, 3, 9]
originalArray = []; // Error: Assignment to constant variable

- 23,004
- 4
- 39
- 73
-
I see, I should be deliberate and code in such a way that strongly implies that an array should not be mutated, but I cannot prevent it completely. Also, regarding my first question, I understand that reassignments are not allowed: such is the nature of const. However, I don't understand the intention of allowing the elements to be changed. I guess my train of thought is if const is meant to make variables immutable why not make it completely immutable? – eaglclaws Mar 08 '22 at 08:34
-
@eaglclaws Thinking about const made me realize I completely forgot `Object.freeze` exists, it might be what you're looking for. See the edited answer. – Alex Mar 09 '22 at 10:20
An array, number, string are all objects. They differ in the way they store values. A String are charcaters and therefore fixed if it has been declared const. An array is a construct of how multiple values are stored. This means that const sets the type imutable but not the values.

- 15,957
- 6
- 40
- 79
-
"*An array, number, string are all objects.*" of these only an array is an object. The strings and numbers (lowercase) are primitives. There are String and Number objects but they aren't immutable in that *the object* can be changed. The respective string or number value held not but that doesn't make the object itself immutable. The primitive values are *converted* to objects briefly in some cases but aren't objects themselves. And they are immutable by virtue of being primitives without any fields or anything. – VLAZ Mar 08 '22 at 09:52
-
To "change" a variable that holds a primitive you have to reassign the variable. Mutable objects assigned to a variable don't require reassignment to be changed. Immutable objects would – VLAZ Mar 08 '22 at 09:52
-
@VLAZ Ok. Thank you very much. I always learn something new. I actually asked myself the same question once and explained it to myself as I wrote it in my answer. – Maik Lowrey Mar 08 '22 at 09:58