This post assumes that you know the differences
If this is not the case,
please read about them elsewhere first.
For some time now,
additional ways to declare variables:
Some programmers have adopted this convention
of always using
const whenever possible.
The usual argument for preferring
const is that
we can more easily reason about the code
since we know the value won’t change.
That would have been really nice,
except it can change.
Although the binding will indeed remain constant within the scope,
for reference types (objects and arrays),
the value being pointed to can still be modified.
This means, if we pass those variable to a function or call its method,
we cannot be certain that it will remain the same afterwards
without knowing what the function does.
As for primitive types, those values won’t change
even if they aren’t
With this limitation,
the only thing we gain by using
a guarantee that the variable won’t be reassigned.
But finding assignments within the same scope is trivial.
It only becomes a pain when
the function becomes too long or does too much,
but the solution to that
would be to break the function down to more manageable parts.
Naming things really is one of the hard things in computer science. All too often we end up with terms kinda similar but different from its use in another context.
For example, the terms “function” and “variable” from mathematics usually follow a slightly different meaning in programming. Instead of a mapping of values, we have a procedure. Instead of a symbol representing a value, we have label for some storage space.
const may lead others to incorrectly assume
that the value is “truly” constant or immutable.
Given that it doesn’t bring us much of an advantage,
it would be better to avoid this potential confusion.
For variables with values which remain the same everywhere
no matter what scope
(like constants in mathematics),
const would be a good choice.
For variables which are local to the function,
I would go for
just because you are using
let doesn’t mean
you have to reassign it.
It is often good to avoid statefulness,
and you should not let
(excuse the horrible pun)