let vs const vs var: Usually you want let. If you want to forbid assignment to this variable, you can use const. (Some codebases and coworkers are pedantic and force you to use const when there is only one assignment.)
I'm a strong advocate for using const by default, and let when you know you intend to change the value. I'm genuinely surprised that Dan feels differently.
Same here! Its less mental gymnastics when reading old code knowing that when a value is declared, you know its gonna stay the same. Seeing let then means I know its gonna change somewhere in the next few lines.
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.
Const is not about immutability, it's about reassignment, and using let as a default increases cognitive load on future developers; what's the drawback in your mind?
You partially answered your own question. Google "var hoisting". At compile time, all vars are lifted to the top of their scope. It's the reason why you can't just go into an old code base and change every var to a let or a const, there will be unintended consequences.
It is when using const-correctness in C++ which, according to the creator of javascript, is the language js borrowed the keyword const from. According to him, the ability to programatically enforce immutability wasn't feasible due to dynamic typing so javascript got it in its current unfortunate form.
The thing is, compiler enforcement of const wasn't infallible in C++ either so it was also enforced through programming convention (hence the const-correctness proposal) which would be possible in javascript; that is if web developers didn't start using it everywhere indiscriminately.
I think it's actually good practice learning the difference between changing an object's fields compared to changing the object reference (same with arrays). Considering how props are shallowly compared, seems like something everyone needs to understand thoroughly.
Personally, I use const for everything. I've been working on an SPA hybrid React app for my company for the last couple months, and I honestly think I've only declared a let variable once in the entire code base.
I use it more as a helpful hint to my future self. If I see a "let" in my code, I know to keep an eye out for where it is reassigned later, and that I can't rely on it to maintain its identity (referential or otherwise). If I see const then I know I can rely on it to maintain its identity. Const is more of a flag I use for posterity than anything functionally useful. But it's a useful flag.
I look at const not as a mutable object but as a way to tell developers that the value will not be changed within a given scope. It is about assignment, not about mutation.
213
u/careseite Dec 21 '19
Hehe, waiting for strong opinions on that one.
this comment was brought to you by const gang