Why const Doesn’t Freeze Your JavaScript Arrays
dev.to·1d·
Discuss: DEV
🏔️Alpine.js
Preview
Report Post

1. Intro - The Common Confusion

Using const with an array feels like it should lock the whole thing in place. Then you try push(), pop(), or update a value, and it still changes. If it’s constant, why is it still changing?

The answer lies in how JavaScript handles variables and references. Once you see that distinction, this behavior becomes far less mysterious and a lot more logical.

2. Binding vs Value — What const Actually Does

A const variable is like a signpost planted firmly in the ground. You can’t move the signpost or point it somewhere else, but whatever it’s pointing at can shift around freely.

Primitives behave like solid stones once placed; they don’t change shape. Arrays and objects are more like workbenches: the signpost stays fixed, but the tools an…

Similar Posts

Loading similar posts...