4 ms·
'const prevents reassignment' is a much simpler way of saying that. Mutability doesn't need to come into it.
by allover 8y ago
'const prevents reassignment' is a much simpler way of saying that.
Mutability doesn't need to come into it.
- duncan-donuts 8y agoI agree with you, but I’ve cleared up this confusion with a number of colleagues. People coming from languages that have a truly immutable “const” find the keyword confusing in js.
- jaredklewis 8y agoYea, I think const in JS alludes to the way const works in C++. If someone knows java though, it can always be explained that const in JS is identical to marking a local variable as final in Java. More so than const, I was perplexed by the JS choice of the `let` keyword. Are there any other languages that use let for declarations that can be reassigned? Did BASIC allow lets to be reassigned? Can’t remember...
- andreareina 8y ago`let` in common lisp can be reassigned.
- mikekchar 8y agoWhile it is simpler, I think it's better that people actually understand the difference between a reference to a value and the value itself. "Pointers are hard" is a statement that many beginner programmers make, but if you don't understand the concept, you will always be limited as a programmer. And whether you say "can be reassigned" or "mutable" (which means exactly he same thing, BTW) it doesn't really matter. JS has some surprising mutability rules. Variable references can either be mutable or not (depending on const, let or var), however function parameter references are always mutable (which can cause much hilarity in some circumstances). Even values are a bit strange at times unless you understand what's going on under the hood. It's obvious that boolean or number values are immutable, but it's less obvious that string values are immutable; even more so since it doesn't throw an error (at least in V8) when you try to mutate them. You might assume that function values are immutable (how could you mutate it?), but because of closures, they are completely mutable (as long as the values being closed over are mutable). And, well, functions are also so-called "object" values in JS (which I still think is not a good idea, but I understand why they did it). While, it is more complex to discuss that separation, it's valuable when you get into more difficult discussions -- especially if you are trying to write mostly pure functional code, with a few non-pure bits for performance. You need to be able segregate the pure from the non-pure and if you are passing closures, for instance, it's super important to understand how mutating a value in one place can essentially infect something that you thought was pure.
- karmakaze 8y ago> "can be reassigned" or "mutable" (which means exactly he same thing, BTW) I have never used 'mutable' to describe a variable, but rather only to what it references. Perhaps it's more lax in js-land. Why would you call something that's atomic ally changed anything other than assignment?
- allover 8y ago> And whether you say "can be reassigned" or "mutable" (which means exactly he same thing, BTW) it doesn't really matter. They don't mean the same thing, so it matters. None of your comment about booleans etc is relevant. `const` prevents reassignment and that is all, it literally has nothing to do with mutability. If you bring mutability into the conversation you've confused something simple.