ES2015 introduced the let
and const
keywords, which essentially have the same semantics apart from reassignment. (const
can be seen as a final
variable in languages like Java.)
I see the point of switching from var
to let
, but many online articles recommend using const
whenever possible (in some form of manual SSA).
However, I recall in the question on Java final variables, the accepted most popular answer adviced against using final
local variables because it makes the code more difficult to understand.
This is reflected in reality as a number of code review comments questioned the "over-use" of const
on local variables.
So does const
still reduces readability in JavaScript?
-
1No, it doesn't reduce readability. In fact, I think it increases readability.Erik Eidt– Erik Eidt2016年04月21日 15:07:43 +00:00Commented Apr 21, 2016 at 15:07
2 Answers 2
*sigh*... This is why immutable needs to be the default. Even the referenced Java answer suggests this. Note that that answer does not recommend removing final
modifiers, just that the author of that answer wouldn't add them in new code (for local variables) because they "clutter up" the code.
However, there is a difference between JavaScript and Java here. In Java, final
is an additional modifier on a variable declaration, so you get:
final int foo = 3; // versus
int foo = 3;
In JavaScript, though, const
is just an alternate variable declaration, so the situation is:
var foo = 3; // (or let nowadays) versus
const foo = 3;
I don't think two more characters constitutes "clutter". If the alternative being suggested is just foo = 3
then the reviewers are just wrong.
Personally, I would always use const
when applicable and would suggest it's inclusion in a code review. But then I'm a Haskeller, so I would be like that. But also, JavaScript tends to be more pervasively mutable and have a worse debugging story when things do unexpectedly change, that I would say const
in JavaScript is more valuable than final
in Java (though it's still valuable there.)
As Ixrec points out, const
only controls the modifiability of the binding, not the object that is bound. So it's perfectly legal to write:
const foo = { bar: 3 };
foo.bar = 5;
This can be used as an argument against using const
as someone may be surprised when an "unmodifiable" object is modified. Newer versions of JavaScript do have a mechanism to actually make objects unmodifiable, namely Object.freeze
. It makes a good addition to const
(though only use it on objects that you create, for example, as Object.freeze({ ... })
). This combination will communicate and enforce your intent. If used consistently, the times where you are mutating things will stick out and clearly signal non-trivial data flow.
-
Admittedly JS
const
doesn't prevent you modifying the value, only reassigning the variable, so the question of whetherconst
is more valuable thanfinal
might be closer to a tie. Completely agree otherwise though, +1.Ixrec– Ixrec2016年04月21日 14:48:12 +00:00Commented Apr 21, 2016 at 14:48 -
@Ixrec That's a good point and can be argued as a reason to not use
const
as it may be interpreted as delivering more than it does.const foo = ...; foo.bar = 3;
probably has a touch of cognitive dissonance to it. (Of course, if objects were frozen as a matter of course... Solution to any problem, more immutability.)Derek Elkins left SE– Derek Elkins left SE2016年04月21日 15:11:37 +00:00Commented Apr 21, 2016 at 15:11 -
Agreed, for public-facing stuff always do
const foo = Object.freeze({ ... });
so it's truly bulletproof. For internal stuff it's imo still reasonable to useconst
overvar
and omit just theObject.freeze()
, since mutating a local variable you shouldn't is a much easier thing to do on accident.Ixrec– Ixrec2016年04月21日 15:16:52 +00:00Commented Apr 21, 2016 at 15:16 -
@Ixrec I think we're on exactly the same page. I was going to add a similar suggestion to my previous comment, but I didn't feel like I needed to tell you, so I'll update the answer. Thanks.Derek Elkins left SE– Derek Elkins left SE2016年04月21日 15:20:35 +00:00Commented Apr 21, 2016 at 15:20
-
I have been fiddling with JavaScript for the past few weeks, getting into Node.js, React and stuff, and I don't see a single point against using
const
whenever applicable either.Andy– Andy2016年04月21日 16:30:07 +00:00Commented Apr 21, 2016 at 16:30
It is indeed good practice to declare variables as const
(or readonly
or final
, depending on the language) if possible.
The author of the linked answer also admits that is is right to mark variables as final
, but the author is simply too lazy to do it.
Even if the authors opinion about "clutter" was right, then it wouldn't apply to JavaScript or TypeScript, since const
is instead of let
, not in addition to.