var declaration is function scoped and let declaration is block scoped.

See https://basarat.gitbook.io/typescript/future-javascript/let for more details.

Answer from MartyIX on Stack Overflow
🌐
Tutorial Teacher
tutorialsteacher.com › typescript › typescript-variable
TypeScript Variable Declarations: var, let, const
... The let declarations follow the same syntax as var declarations. Unlike variables declared with var, variables declared with let have a block-scope. This means that the scope of let variables is limited to their containing block, e.g.
Discussions

var, const, let
Rule of thumb: start with const, if you need to change the value later, change to let, and forget about var. More on reddit.com
🌐 r/typescript
23
9
April 24, 2018
Difference between var, let and const
Using the function keyword creates a function declaration. Function declarations are hoisted to the top of the scope, thus the following code works fine: add(1, 2) function add (a, b) { return a + b } There are readability advantages to using the function keyword as it clearly communicates that this is a function. We can also create an anonymous function expression and, because functions in JavaScript are first class citizens, assign a function to a named variable with let, const or var. Keep in mind though, variables declared with let and const are hoisted, but not initialized with a value. Whereas variables declared with var are hoisted and initialized with the value undefined. So because add is invoked prior to it's initialization in the code below we encounter an error. With let/const: add(1, 2) // Reference error - cannot access 'add' before initialization const add = function (a, b) { return a + b } With var: add(1, 2) // Type error - 'add' is not a function (as it is initialized as undefined when hoisted) var add = function (a, b) { return a + b } If you're assigning a function to a variable I can't think of any situation where you wouldn't use const. Functions should always be pure and immutable whenever possible. Given a certain input, the output should always be consistently the same. Using const protects you from accidentally reassigning a variable containing a function expression. Compare this to use of the function keyword, which allows you to declare a named function twice, overiding the previous: function add (a, b) { return a + b } function add (a, b) { return a - b } add(2, 1) // 1 This is a disadvantage of function declarations versus expressions. Provided you understand the pros/cons and how hoisting works, it is really personal choice whether you prefer function declarations versus expressions. More on reddit.com
🌐 r/learnjavascript
31
57
March 28, 2022
People also ask

What are the main differences between var and let in JavaScript?
The main differences lie in their iterations, scope, access, declaration, and re-declaration. For example, the var keyword has a global scope, while the let keyword is limited to block scope.
🌐
testbook.com
testbook.com › home › key differences › difference between var and let in javascript - testbook
Difference Between var and let in JavaScript - Testbook
What is the var keyword in JavaScript?
The var statement assists a user in declaring any variable in JavaScript. A variable declared with this keyword stays defined throughout a given program.
🌐
testbook.com
testbook.com › home › key differences › difference between var and let in javascript - testbook
Difference Between var and let in JavaScript - Testbook
What is the let keyword in JavaScript?
The let statement assists a user in declaring any local variable in TypeScript. The var keyword is similar to the let keyword, but there are some restrictions in the let keyword as compared to the var keyword.
🌐
testbook.com
testbook.com › home › key differences › difference between var and let in javascript - testbook
Difference Between var and let in JavaScript - Testbook
🌐
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 - In JavaScript and TypeScript, var, let, and const are the three keywords used to declare variables. However, all three have different characteristics, in regards to scoping, hoisting, and mutability.
🌐
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.
🌐
Testbook
testbook.com › home › key differences › difference between var and let in javascript - testbook
Difference Between var and let in JavaScript - Testbook
The let keyword, on the other hand, is used to declare a local variable in TypeScript. While similar to var, let has certain restrictions that var does not. It enhances code readability and reduces the likelihood of programming errors.
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › javascript › difference between `let` and `var` in javascript
Difference between `let` and `var` in JavaScript | Sentry
A var variable will be available thoroughout the function body in which it is defined, no matter how deeply nested its definition. A let variable will only be available within the same block where it is defined.
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › typescript let vs var vs const
Typescript Let vs Var vs Const - Tektutorialshub
March 15, 2023 - The following example shows how to declare a variable using the above keywords. You can learn more about the variable declaration from Typescript variables tutorial. 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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-var-and-let-in-javascript
Difference between var and let in JavaScript - GeeksforGeeks
Code 2: In the following code, clicking start will call a function that changes the color of the two headings every 0.5sec. The color of the first heading is stored in a var and the second one is declared by using let. Both of them are then accessed outside the function block.
Published   July 11, 2025
🌐
Mimo
mimo.org › glossary › typescript › let
TypeScript Let: Syntax, Usage, and Examples
Use let in TypeScript to declare block-scoped, mutable variables. It’s ideal when values change—safer and more predictable than var.
🌐
Mimo
mimo.org › glossary › typescript › var
TypeScript Var: Syntax, Usage, and Examples
Because var is function-scoped, not block-scoped, the variable i isn’t reset on each loop iteration. This causes all callbacks to log 3, not 0, 1, and 2. You can fix this by using let instead. When you use var, TypeScript hoists the declaration (but not the initialization) to the top of the function or global scope.
🌐
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 - Explanation: In the example, var hoists the variable declaration to the top of the function, making it accessible before the line where it’s actually assigned. However, only the declaration is hoisted, not the assignment, so the first console.log returns undefined. let was introduced in ES6 to address some of the limitations of var.
🌐
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 - This article explores the contrasts ... features. While var offers familiarity but carries risks such as hoisting, let embraces block scoping, enhancing code readability and preventing redeclarations....
🌐
www.java4coding.com
java4coding.com › contents › typescript › typescript-let-vs-var
TypeScript let vs var - java4coding
A variable declared with var has function scope. When declared in a function, the variable can be accessed everywhere in the function. A variable declared with let or const has block scope.
🌐
GitBook
basarat.gitbook.io › typescript › future-javascript › let
let | TypeScript Deep Dive
March 18, 2020 - That is if you use let instead of var you get a true unique element disconnected from what you might have defined outside the scope.
🌐
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 - In TypeScript, another benefit of const is that when you use it to create a variable, TypeScript assigns it a stricter type. const var1 = 'foo'; //type is 'foo' let var2 = 'bar'; // type is string
🌐
HowToDoInJava
howtodoinjava.com › home › typescript › difference between let, var and const
Difference between let, var and const in JavaScript
September 22, 2021 - Variable hoisting means that if ... that variable before being declared. ... The let keyword is very similar syntax to the var keyword – but it is more restrict in the scoping....
🌐
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 - The let statement declares a block-scoped local variable, optionally initializing it to a value.
🌐
W3Schools Blog
w3schools.blog › home › difference between var and let
Difference between var and let - W3schools
February 27, 2018 - The var statement is used to declare a variable. We can optionally initialize the value of that variable. Example: var num =10; The let statement is used to declare a local variable in a block scope. It is similar to var, in that we can optionally initialize the variable.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-use-let-or-var
The Truth About Using `let` and `var` in TypeScript
In earlier versions of JavaScript (and TypeScript), var was the default way to declare variables. However, with the introduction of ES6 and the new let and const keywords, developers began to favor let for its block-scope capabilities.