简体   繁体   中英

Benefit of const vs let in TypeScript (or Javascript)

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?

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.

When you have a variable which can be declared as const , and you declare it as such you inform the reader that you don't plan to reassign it to a different value later on.

Also by declaring a variable const it means you have thought up front that you don't plan to reassign it, which in turn can protect you from accidental bugs or errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM