DEV Community

Once and for all: const in JavaScript is not immutable

Valentino Gagliardi on July 12, 2019

I know that's been said hundred of times, but I still see people proclaiming (even in some JavaScript books) that const is immutable. It is not. ...
Collapse
 
itsjzt profile image
Saurabh Sharma

The thing is const never let you to change the memory address it refers to.

you but can mutate the value itself.

Collapse
 
skhmt profile image
Mike πŸˆβ€β¬›

Const makes the reference immutable, and for primitives that means the value is immutable too.

But for Objects, the difference matters.

Collapse
 
valentinogagliardi profile image
Valentino Gagliardi

yes!

Collapse
 
prahladyeri profile image
Prahlad Yeri

That's just because of the nature of JavaScript, that's how it is.

In JavaScript, some built-in types (numbers, strings) are immutable, but custom objects are generally mutable.

So, it matters not whether you've declared them as const or not, what matters is whether they are custom objects (like in your example) or simple numbers/strings. The latter are obviously immutable whether you use const or not.

And even with your custom object, you can make individual attributes immutable like this (again, const doesn't matter here):

Object.defineProperty(person, 'name', { value: 'John', writable: false });

So when you do this, it'll be silently ignored since the person object is now immutable:

person.name = "Valentino";

This is irrespective of whether you defined person as let, const or var!

Collapse
 
valentinogagliardi profile image
Valentino Gagliardi

My point is that there are people out there still saying "const is intrinsically immutable". While it's not. As for objects >> Protecting objects from manipulation

Collapse
 
nicknapoli82 profile image
Nicholas Napoli • Edited

I would argue that const is intrinsically immutable. Posing the argument that making a reference immutable, but the data inside is not, is not relevant. Granted. It would be nice if this were default behavior, but knowing why the data inside is not const is important.

This flows down to the engine that js is running on in lower level language land, and that land explains this concept of why well. It should not be a debate.

Collapse
 
georgecoldham profile image
George

If you want immutability then you need to start using Object.freeze() and Object.seal()

Collapse
 
valentinogagliardi profile image
Valentino Gagliardi

Object.freeze is shallow though :-)

Collapse
 
itsjzt profile image
Saurabh Sharma • Edited

So that's why there libraries like Immutable.js, but it would be great if JS has something for making values immutable.

Collapse
 
thebuzzsaw profile image
Kelly Brown

It's all a war of semantics. Technically, the variable itself is immutable for its lifetime; it just so happens to act as a gateway to data that is quite mutable. This is why I hate the way Java tries to teach students: "Everything is pass by value." Yeah, everything is pass by value... but the value happens to be a reference to something else. Strange that something passed by "value" lets me trigger side effects for observers of that data.

Good post.

Collapse
 
conectado profile image
Conectado

I think const as an immutable pointer. You can change the value of what it points to but not the place it points to.

Disclaimer, that is just how I think about it, I don't know if it works like that internally.

Collapse
 
laas29_97 profile image
laas29

We have to be sure what exactly const are pointing to, the memory address remains the same.

Collapse
 
ben profile image
Ben Halpern

I just learned so much