Why most of the time should I use const
instead of let
in JavaScript? As we know if we use const
then we can\'t reassign value later.
Using const
by default is essentially a matter of programming style. However there are pros and cons to each:
const
by defaultThe arguments in favor of using const
by default are the following:
const
variable can count on the certainty that it will not be reassigned;Here is an example of advantage when using const
with TypeScript:
const hello = "Hello" as string | undefined
if (hello !== undefined) {
["A", "B"].forEach(
name => console.log(`${hello.toUpperCase()}, ${name}`) // OK
)
}
With let
, in strict
mode, TypeScript detects an error:
let hello = // …
// …
name => console.log(`${hello.toUpperCase()}, ${name}`)
// ^__ error here: Object is possibly 'undefined'.
let
by defaultI summarize here the article Use “let” by default, not “const” that gives arguments in favor of using let
by default rather than const
:
let
, because it is more expressive to reserve const
for real constants;const
is misleading because it doesn’t stop references being modified;const
by default is inconsistant with function parameters;const
.Basically,
let
if the variable's value will change during the codeconst
if it won't and you / your team want to use const
in those situations in the project you're working on; it's a matter of styleIf you do use const
, then it's surprising how often it turns out that the guidelines above mean you use const
because you end up not needing to change a variable's value (if you're following the usual rules of keeping your functions of reasonable size and such). (Well, it surprised me, anyway...)
Using const
when the variable's¹ value is not meant to change accomplishes a few things:
It tells others reading your code that you don't intend the value to change.
It gives you a nice proactive error if you change the code so it writes to that variable. (A decent IDE can flag this up proactively, but if not, you'll get the error when running the code.) You can then make an informed, intentional decision: Should you change it to let
, or did you not mean to change that variable's value in the first place?
It gives a hint to the JavaScript engine's optimizer that you won't be changing that variable's value. While the engine can frequently work that out through code analysis, using const
saves it the trouble. (Caveat: I have no idea whether this is actually useful to the JavaScript engine. It seems like it would be, but runtime code optimization is a very complicated and sometimes non-intuitive process.)
¹ Yes, it's funny to use the term "variable" to refer to something that by definition doesn't vary. :-) The specification's term is "binding," but I bet you won't hear people talking about "bindings" in everyday conversation anytime soon... So the aggregate term will probably remain "variable" except when we can specifically refer to something as a "constant."
You can use const
when you want to declare a non-editable variable.
Meanwhile let
is used when you have an editable variable.
It also can be used for variable scoping.
See : let and const
const
most of the time?Because the const
declaration creates a read-only reference to a value (cannot be reassigned).
It prevents us to be confused with the value of the variable.