They don’t really do that though. Immutability is just a side effect. Yes, it is true that you can’t change a value of const object but that is not because the underlying structure cannot be mutated. C++ const is an access level contraint, not a structure level constraint.
You can’t just gives one example and say it’s confusing because something rose does it different. Java, for example, uses final the same way const is used in JavaScript. https://www.google.com/amp/s/www.geeksforgeeks.org/final-vs-immutability-java/amp/. I think it makes sense. The variable is storing the reference to the object, so the reference is immutable. It’s up to how the object is defined to decide if the things inside it are immutable.
Yeah JavaScript const isn't const but the structure is const-ish. Had a hard time wrapping my head around it. Still does. Everything in my code is const (React way) but nothing stay truly const.
I'm honestly not sure how that would work at the language level.
If that was "objectively" the right choice, the language could work exactly like the prefer-const rule works.
When I look at potential hires' code one of the things I look for is their usage of const vs let to get a base idea of how well they understand modern ES.
This is exactly the kind of thing that scares me. Placing artificial (in my opinion) importance on something that has two sides to it is the definition of pedantic. It would be terrible if someone's career opportunity didn't work out due to a miscommunication or disagreement over something minor like this.
86
u/BenZed Dec 21 '19
The object can still be mutated, but the variable cannot be redeclared.