Disclaimer: I know that the question below might be seen as "asking for opinions" (so it'd be off-topic).
But it's not the case: I'm looking for pros and/or cons facts which should be took in account while thinking about that.
By chance I just read (from the excellent Exploring ES6) an excerpt focusing on const
vs let
vs var
, where the conclusion states (bolding is mine):
Then we have two approaches:
- Prefer
const
:const
marks immutable bindings.- Prefer
let
:const
marks immutable values.I lean slightly in favor of #1, but #2 is fine, too.
What puzzles me is: this preference seems to be based on the deep technical significance of the resulting difference betweeen the two approaches, as if the author feels essentially concerned by how the code works at low level.
But I feel concerned also (and maybe first!) by the readability aspect: from this point of view, #2 seems better to make the code more semantically significant about what happens to the processed data.
But maybe I'm missing some advantages of the #1 choice...?
EDIT, taking advantage of the link proposed as duplicate: Why most of the time should I use const instead of let in javascript?
In fact the accepted answer to that question actually exposes things in such a way that it rather enforces me to my previous view, i.e. #2 is better than #1.
BTW I realized that my question was probably not as clear as it should, so here is a more detailed rewording:
- I'm quite aware of (AFAIK) all the technical differences between
const
andlet
, so my question doesn't ask for anything about that. - The only point really involved by the choice between #1 and #2 lies in using
const
for any immutable object (though its content may be changed!) vs for frozen objects only.
It's there that I'm surprised the author prefers #1, since (from my point of view) it's rather misleading.
The most frequent way of using objects is that their content changes during the process, so reading const
at its declaration level, if #1 is used:
- at best we don't know what is supposed to happen to it.
- at worst we may imagine that its content will never change, while it can!
In the other hand, choosing to use #2, we can trust that a const
-declared object will not change (indeed apart from oversight and/or bug).
So to go back to my question: since the above reflexion seems to clearly lead to choose #2, I wonder what point I may have missed, which makes the author prefer #1.