Benefit from const vs let in TypeScript (or Javascript)

I read TypeScript Deep Dive , and I see that both let and const are block regions, which is Great. Obviously, the constant cannot be changed (it is unchanged). But why does ReSharper prompt me to change as much as possible let to const ? I assume ReSharper believes that when using const over let there is an increase in performance? Is there any speed difference between const and let ? Is there any other 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; } 

I used to have 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 anyway?

I also found this question on SO , but it talks more about what let and const , and not about why one is better than the other, He really uses const as much as possible, but what advantage does this give?

+5
source share
2 answers

I agree with Giorgi that performance is not the main reason. The code analyzer could also pinpoint that a variable declared with let is never reassigned and will optimize it just as if you had declared it with const . (Heck, lintra have rules to detect this and suggest using const instead of let .)

Yes, this signals the reader that you are not going to assign this variable. The advantage of const over a comment saying the same thing is basically that const is the standard way of signaling. Being standard, it conveys information more easily than user comments. (Also, the comment may be wrong, but const will not allow you to make a mistake.) I do not think this is the main advantage, though.

The principle of least privilege is often invoked along with const , but why should I care about least privilege? Because it helps with early detection of coding errors . 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 wanted to type found = child . Yes, the error can be found during testing. But why wait for testing? I never wanted a tree change. If I noted its const , I would immediately recognize the error, because the compiler informed me of this. I would not have to wait for testing. The above code is pretty simple, but imagine a more complex algorithm that uses more variables.

+8
source

IMO I don't think this is due to performance considerations, but it could be due to documentation.

If you have a variable that can be declared as const , and you declare it as such, you inform the reader that you do not plan to reassign it to another value later.

In addition, this may be due to something like the principle of least privilege, when you declare a variable as const , you later protect it from subsequent modification of the accident.

+4
source

Source: https://habr.com/ru/post/1264300/


All Articles