I was reading the TypeScript Deep Dive and I see that both let
and const
are block scoped, which is great. Obviously, a const cannot be changed (it is immutable). But why is ReSharper is encouraging me to change as many let
s to const
as I can? I'm assuming ReSharper thinks there is a performance gain in using const
over let
? Is there a speed difference between const
and let
? Is there a different reason to use const
? Take the following example:
for (let i = 0; i < this.InputControl.ParentQuestions.length; i++) { const id = this.InputControl.ParentQuestions[i].ParentId; const value = this.InputControl.ParentQuestions[i].ParentRequiredAnswer; if (!formVals[id]) return false; if (formVals[id].toLowerCase() != value.toLowerCase()) return false; }
Previously, I had let id
and let value
but ReSharper asked me to change them to const
, which works, but why is it better in this case? Or in any case?
I also found this question on SO, but it talks more about what let
and const
do, not so much why one is better than the other. It does say to use const
as much as possible, but what benefit does that provide?
Use var and let to define any variable, with or without type or initial value. We use the const keyword initialize a constant whose value does not change. Hence we must initialize it with a value. You can not use it to declare a variable where there is no initial value.
`const` is a signal that the identifier won't be reassigned. `let`, is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it's defined in, which is not always the entire containing function.
The execution context underlying how the JavaScript interpreter runs the code is basically the same when you use var compared to when you use let and const . That results in the same execution speed.
The scope of a let variable is block scope. The scope of a const variable is block scope. It can be updated and re-declared into the scope. It can be updated but cannot be re-declared into the scope.
I agree with Giorgi that performance is not the main reason. A code analyzer could just as well determine that a variable declared with let
is not ever reassigned and optimize it the same as if you had declared it with const
. (Heck, linters have rules to detect this and suggest using const
instead of let
.)
Yes, it does signal to the reader that you're not going to assign to the variable. The benefit of const
, over putting a comment saying the same thing, is mainly that const
is a standard way of signalling it. Being standard, it transmits the information more readily than custom comments. (Also, a comment could be wrong but const
won't let you be wrong.) I don't think this is the main benefit though.
The "principle of least privilege" is often invoked in conjunction with const
, but why should I care about the least privilege? Because it helps with early detection of coding mistakes. Consider the following code:
function findSomethingInDocument(document) { let tree = getTreeFromDocument(document); // We get some sort of tree structure. let found; for (const child of tree.children) { if (child.someFlag) { tree = child; // Oops I meant found = child :( break; } } return found; }
In this code, I typed tree = child
when I meant to type found = child
. Yes, the bug can be found in testing. But why wait for testing? I never meant tree
to be changed. If I had marked it const
then I would have immediately learned the error because the compiler would informed me of it. I would not have to wait for testing. The code above is fairly simple but imagine a more complicated algorithm that uses more variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With