Const and Array Mutability
What you'll learn: How const arrays can have elements added or removed even though the reference is constant.
Arrays Behave Just Like Objects
You already learned that const prevents reassigning an object but doesn't stop you from changing its properties. Arrays work exactly the same way because arrays are objects in JavaScript.
When you declare an array with const, you're locking the reference to that array in memory. You can't point the variable to a different array, but you can freely modify the contents of the existing array.
Mutating a Const Array
const colors = ['red', 'blue'];
// ✅ These work — you're modifying the array's contents:
colors.push('green'); // ['red', 'blue', 'green']
colors[0] = 'yellow'; // ['yellow', 'blue', 'green']
colors.pop(); // ['yellow', 'blue']
// ❌ This fails — you're trying to reassign:
colors = ['purple']; // TypeError!
The variable colors always points to the same array in memory, but that array's elements can change.
The Analogy
Think of const like a house address written in permanent ink. The address never changes (you can't reassign it), but the furniture inside the house (array elements) can be rearranged, added, or removed at any time.
Why This Matters
This behavior is identical to what you saw with objects. Whether working with objects or arrays, const protects the reference, not the contents. Understanding this prevents confusion when your "constant" arrays appear to change.
Key Takeaway: const arrays can have elements added, removed, or changed because the array reference remains constant while its contents are mutable.