const: Reference Binding and ImmutabilityDeclaring an array with const means the variable name cannot be reassigned to a different array. However, const does not freeze the array items inside, so you can still update elements or call push().
const Allows push() and Element EditsA const binding stores a fixed memory pointer to one array instance. Mutating elements inside that same instance keeps the pointer unchanged, whereas writing items = [] tries to point the variable somewhere else and throws a TypeError.
const ArraysWhen you want to preserve original state, combine const with non-mutating methods like spread [...arr], filter(), toSorted(), or Object.freeze().
const tags = ["javascript", "web"];
// Allowed: mutating contents of the same array
tags.push("es6");
tags[0] = "js";
console.log("Mutated const array:", tags);
// Preferred immutable pattern: build a new array
const nextTags = [...tags, "node"];
console.log("New copy:", nextTags);
// Shallow freeze blocks push() and index assignments
const frozenList = Object.freeze(["read", "write"]);
console.log("Frozen list:", frozenList);
const ArraysJust like let, a const array lives only inside its { ... } block. An array declared inside an if block or function never collides with a const array of the same name outside that block.
const array without = [...]: const items; throws a SyntaxError because every const needs an initial value.const array after filter(): Writing items = items.filter(...) fails with TypeError: Assignment to constant variable. Store the filtered result in a new const name instead.Object.freeze(arr) freezes nested objects: Freezing an array of objects blocks adding or removing array elements, yet properties inside arr[0] can still change.const?Yes, almost all arrays should use const because you rarely replace the array variable binding itself. Use let only when you intentionally reassign the variable to a new array.
const array?JavaScript throws TypeError: Assignment to constant variable. at runtime when executing myConstArray = [].
const array without mutating it?Create a new const variable using filter(), such as const activeUsers = users.filter((u) => u.id !== removedId);.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.