JavaScript const — Does it prevent variable change?
This is a daily Javascript challenge from the CodeShot archive. Practice your knowledge of Const Immutability Myth Objects and improve your technical interview readiness.
Detailed Explanation
Why This Question Matters
If you're just starting with JavaScript, you'll see const everywhere. Most tutorials tell you it stands for "constant" and that it means the variable can't change. On the surface, that sounds simple. But in JS, "change" is a slippery word.
This is one of those classic "gotcha" questions. If you answer "True" to the question *"const means the variable cannot change,"* you're technically wrong. Why? Because there is a massive difference between reassigning a variable and mutating a value.
Getting this wrong leads to bugs that are a nightmare to debug—like when you "freeze" an object with const but your data still changes magically in the background.
Understanding the Code
Since we don't have a snippet, let's look at the two scenarios that define how const actually works.
First, the simple case: Primitives.
With strings, numbers, and booleans,
const behaves exactly how you'd expect. You can't change the value. The variable is locked.
But then we hit Reference Types (Objects and Arrays). This is where the confusion starts:
Internally, const doesn't make the *value* immutable; it makes the *binding* immutable.
Think of a variable as a pointer. When you use const with an object, you are locking the pointer to a specific address in memory. You can't point that variable at a different address (reassignment), but you can absolutely change the stuff inside that address (mutation).
Finding the Correct Answer
The question asks: *TRUE or FALSE: const means the variable cannot change.*
The answer is FALSE.
Here is the breakdown:
- If the variable holds a primitive (like 5 or "Hello"), it cannot change.
- If the variable holds an object or array, the contents of that object or array can still be modified.
Because const does not guarantee that the value itself remains unchanged in all cases, the blanket statement "the variable cannot change" is false. It only means the variable cannot be reassigned.
Common Mistakes Developers Make
The biggest mistake juniors make is assuming const is a security blanket for data integrity.
I've seen developers push an item into a const array and then wonder why their "constant" list is suddenly longer.
Another common trap is thinking that
const makes an object "read-only." It doesn't. If you pass a const object into a function, that function can still change the properties of that object, and those changes will persist throughout your app.
If you actually want a value that cannot change, const isn't enough. You need Object.freeze().
Real-World Usage
In a professional codebase, you'll notice we use const for almost everything by default. We only use let if we know the value *must* be reassigned (like a loop counter or a toggle).
Why? Because it reduces cognitive load. When I see const, I know that the variable name will always point to the same thing. I don't have to scan the rest of the function to see if the variable was suddenly swapped out for a different object or a null value.
However, in state management (like Redux or React hooks), we treat const objects as if they *were* immutable. We don't do user.name = 'Sam'. Instead, we create a new copy:
This isn't because
const forces us to, but because mutating state directly leads to unpredictable UI bugs.
Key Takeaways
- const prevents reassignment, not mutation.
- For primitives (numbers, strings), const effectively makes the value unchangeable.
- For objects and arrays, you can still change the properties or elements inside them.
- Use const by default to make your code more predictable.
- If you need a truly immutable object, use Object.freeze().
Why this matters
Understanding Const Immutability Myth Objects is crucial for passing technical interviews. In real-world applications, this concept often leads to subtle bugs if not handled correctly. For more details, you can always refer to the official MDN Documentation.