There are some very important benefits to the use of const
and some would say it should be used wherever possible because of how deliberate and indicative it is.
It is, as far as I can tell, the most indicative and predictable declaration of variables in JavaScript, and one of the most useful, BECAUSE of how constrained it is. Why? Because it eliminates some possibilities available to var
and let
declarations.
What can you infer when you read a const
? You know all of the following just by reading the const
declaration statement, AND without scanning for other references to that variable:
The following quote is from an article arguing the benefits of let
and const
. It also more directly answers your question about the keyword's constraints/limits:
Constraints such as those offered by
let
andconst
are a powerful way of making code easier to understand. Try to accrue as many of these constraints as possible in the code you write. The more declarative constraints that limit what a piece of code could mean, the easier and faster it is for humans to read, parse, and understand a piece of code in the future.Granted, there’s more rules to a
const
declaration than to avar
declaration: block-scoped, TDZ, assign at declaration, no reassignment. Whereasvar
statements only signal function scoping. Rule-counting, however, doesn’t offer a lot of insight. It is better to weigh these rules in terms of complexity: does the rule add or subtract complexity? In the case ofconst
, block scoping means a narrower scope than function scoping, TDZ means that we don’t need to scan the scope backwards from the declaration in order to spot usage before declaration, and assignment rules mean that the binding will always preserve the same reference.The more constrained statements are, the simpler a piece of code becomes. As we add constraints to what a statement might mean, code becomes less unpredictable. This is one of the biggest reasons why statically typed programs are generally easier to read than dynamically typed ones. Static typing places a big constraint on the program writer, but it also places a big constraint on how the program can be interpreted, making its code easier to understand.
With these arguments in mind, it is recommended that you use
const
where possible, as it’s the statement that gives us the least possibilities to think about.