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.

Answer from Craig W. on Stack Overflow
🌐
Tutorial Teacher
tutorialsteacher.com › typescript › typescript-variable
TypeScript Variable Declarations: var, let, const
This increases the code readability and maintainability. Variables can be declared using const similar to var or let declarations. The const makes a variable a constant where its value cannot be changed.
🌐
Medium
medium.com › @robinviktorsson › understanding-the-differences-between-var-let-and-const-in-javascript-and-typescript-0ddd90c0b672
Understanding the Differences Between var, let, and const in JavaScript and TypeScript 💻 | by Robin Viktorsson | Medium
March 10, 2025 - ... Use var only if you are working with legacy JavaScript code or need a function-scoped variable (though this is rare in modern JavaScript/TypeScript). Use let when you need a block-scoped variable that can be reassigned, especially within ...
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › typescript let vs var vs const
Typescript Let vs Var vs Const - Tektutorialshub
March 15, 2023 - There are four ways you can declare a variable. They are ... 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.
🌐
TypeScript
typescriptlang.org › docs › handbook › variable-declarations.html
TypeScript: Documentation - Variable Declaration
In other words, they have the same scoping rules as let, but you can’t re-assign to them. This should not be confused with the idea that the values they refer to are immutable. ... Unless you take specific measures to avoid it, the internal state of a const variable is still modifiable. Fortunately, TypeScript allows you to specify that members of an object are readonly.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-var-let-and-const-keywords-in-javascript
Difference between var, let and const keywords in JavaScript - GeeksforGeeks
This happens because let variables are hoisted but not initialized, so they remain in the TDZ until the declaration is executed. ... Hoisting with const: The variable x is declared with const, which is block-scoped and not hoisted.
Published   November 11, 2021
Find elsewhere
🌐
Home
javahabit.com › typescript-variable-let-var-const
TypeScript's Variable Wars: Battle of let vs. const vs. var – Which One Wins?
May 28, 2023 - In this case, an attempt to access the message variable before its declaration results in a ReferenceError, clearly indicating that variables declared with let must be defined before they are used. The const declaration in TypeScript serves a specific purpose: defining immutable variables that hold constant values.
🌐
freeCodeCamp
freecodecamp.org › news › var-let-and-const-whats-the-difference
Var, Let, and Const – What's the Difference?
April 2, 2020 - Just like var, let declarations are hoisted to the top. Unlike var which is initialized as undefined, the let keyword is not initialized. So if you try to use a let variable before declaration, you'll get a Reference Error.
🌐
Medium
medium.com › swlh › typesript-let-vs-const-vs-var-a09de69998ea
[TypeScript] Let vs Const vs Var. When to use each of these? | by FAM | The Startup | Medium
October 2, 2020 - Well, there is no problem with the code since the var keyword is not block-scoped. This means there is no scope for variables that are declared with var, whether they are declared inside or outside of a loop or a condition. To resolve this problem, you need to declare you variable with let or const.
🌐
Medium
medium.com › @iamsounak01 › understanding-let-const-and-var-in-typescript-a-beginners-guide-with-examples-e99ab6c6fc62
Understanding let, const, and var in TypeScript: A Beginner's Guide with Examples | by Sounak Das | Medium
November 8, 2024 - Immutable Binding: You cannot reassign a const variable. However, if the variable holds an object or array, you can still modify the properties of the object or the elements of the array. Block Scope: Like let, const is block-scoped and does not hoist.
🌐
Upmostly
upmostly.com › home › typescript › exploring the differences between var, let, and const
TypeScript Demystified: Exploring the Differences between var, let, and const - Upmostly
March 28, 2023 - This behaviour makes let a safer and more predictable way to declare variables, especially in larger codebases. It also helps prevent accidental variable overwriting, which can be a common source of bugs in Typescript. Finally, there’s const. Like let, const is block-scoped, but there’s one big difference: once you’ve assigned a value to a const variable, you can’t reassign it.
🌐
Total TypeScript
totaltypescript.com › let-and-const
How let and const Work In TypeScript | Total TypeScript
February 26, 2024 - TypeScript is mirroring JavaScript's treatment of const in order to prevent possible runtime errors. When you declare a variable with const, TypeScript infers it as the literal type you specified. So, TypeScript uses how JavaScript works to its advantage. This will often encourage you to use const over let when declaring variables, as it's a little stricter.
🌐
HowToDoInJava
howtodoinjava.com › home › typescript › difference between let, var and const
Difference between let, var and const in JavaScript
September 21, 2021 - Variables declared by var and const keywords are function scoped and are scoped to the immediate function body. Also, variables declared with var keyword are hoisted (initialized with undefined before the code is run) which means they are accessible ...
🌐
Timoner
timoner.com › home › javascript › var, let and const in javascript: the infernal trio explained (with a touch of typescript)
Var, let and const in JavaScript: The Infernal Trio Explained (with a Touch of TypeScript) | Sébastien TIMONER · Engineering insights
May 17, 2025 - For let, it stays quite broad: the variable can change, so the type is the basic one (string, number, etc.). Constants and literal types: Where it gets fun is that with const, TypeScript will infer a literal type.
Top answer
1 of 2
56

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.

2 of 2
16

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.

🌐
Fireship
fireship.dev › var-let-const
var vs let vs const in JavaScript
However, the only difference is that once you've assigned a value to a variable using const, you can't reassign it to a new value. ... The take away above is that variables declared with let can be re-assigned, but variables declared with const ...
🌐
DEV Community
dev.to › steckdev › exploration-of-var-vs-const-in-nodejs-application-j20
Exploration of var vs const in NodeJS Application - DEV Community
March 1, 2023 - When a variable is declared with const, the compiler can determine that it will never change, and can optimize the code accordingly. ... In this code, myNumber is declared with let, which means that it can be re-assigned.