JS Array Const
JavaScript Array const: Reference Binding and Immutability
Declaring 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().
Why const Allows push() and Element Edits
A 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.
Working Safely with const Arrays
When 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);
Block Scope with const Arrays
Just 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.
Common Mistakes to Avoid
- Declaring
constarray without= [...]:const items;throws aSyntaxErrorbecause everyconstneeds an initial value. - Reassigning a
constarray afterfilter(): Writingitems = items.filter(...)fails withTypeError: Assignment to constant variable. Store the filtered result in a newconstname instead. - Assuming
Object.freeze(arr)freezes nested objects: Freezing an array of objects blocks adding or removing array elements, yet properties insidearr[0]can still change.
Frequently Asked Questions
Should I always declare arrays with 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.
What error happens if I reassign a const array?
JavaScript throws TypeError: Assignment to constant variable. at runtime when executing myConstArray = [].
How do I safely remove an item from a const array without mutating it?
Create a new const variable using filter(), such as const activeUsers = users.filter((u) => u.id !== removedId);.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.