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.
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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 › 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.
🌐
GitBook
basarat.gitbook.io › typescript › future-javascript › let
let | TypeScript Deep Dive
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.
🌐
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.
🌐
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 21, 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.
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › javascript faq
What are the differences between `var` and `let`? Are they necessary?
May 6, 2019 - When creating a variable, do you need to use let or var at all if the variable ISN’T going to be a constant? The code still seems to record the variable just fine, and allow changes, whether I use var/let or not. ? Same with spaces between the +, -, / etc. The code seems to work just fine ...