Const Arrays
In JavaScript, the const keyword prevents a variable from being reassigned using the = operator. It does not mean the value itself is immutable (unchangeable).
Because arrays (like objects) are reference types, you can freely mutate their contents even if they were declared with const.
Modifying Const Arrays
You can add, remove, and mutate elements inside a const array perfectly fine.
const drinks = [];
// Adding an element works perfectly
drinks.push("lemonade");
console.log(drinks); // Output: ["lemonade"]
// Mutating an existing element works perfectly
drinks[0] = "soda";
console.log(drinks); // Output: ["soda"]
Reassigning Const Arrays
The only thing you are prevented from doing is using the assignment operator (=) to point that variable name to a brand new array in memory.
const drinks = ["lemonade"];
// Attempting to overwrite the variable throws a TypeError!
drinks = ["root beer"];
// TypeError: Assignment to constant variable.
This behavior is a common source of confusion for developers coming from languages where “const” implies deep, strict immutability. Just remember: const locks the binding, not the contents.