Use let when the variable's value can be changed.
Use const when the variable's value cannot/should not be changed.
Do not use var.
I confuse the following term var, const, let in typeScript and when should I use them?
Videos
const stands for constant, and it means the variable cannot be reassigned at a later time.
let is similar to var except that it is block scoped, which means it can be declared inside of a for loop and will be local to the body of that for loop (and therefor does not exist outside of it)
The latter is different from a var variable which can be declared anywhere but is always local to the function scope.
In general it is good practice to try and define your variables as const as much as possible.
As I meanwhile do almost exclusivey functional programming I find the destinction between const and let pretty useless.
I never ever reassign values to names. Never - regardless of the language.
Having said that I find the usage problematic due to 2 reasons.
constis longer thanlet(no seriously!)constsort of communicates back: Hey! I am constant but that is not true (well it is, but ...) I had multiple times collegues that were totally surprised
that this is possible
const x = { foo: "bar" }
x["foo"] = "Not bar!"
Sure the name and its reference is const but the object referenced is not. Granted in Typescript you can at least create
type ROO = Readonly<SomeType>
const x: ROO = someReferenceValue
x.someProp = "A wanna be a bar!" //compile error
So for Typescript const finally could mean const
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.
var declaration is function scoped and let declaration is block scoped.
See https://basarat.gitbook.io/typescript/future-javascript/let for more details.
example:
// demo: var
for(var i =0; i<5; i++){
console.log(i)
}//finally i =5
console.log(i) // i=5
// demo: let
for(let i = 0; i<5; i++){
console.log(i)
}
console.log(i)// i is undefined