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 OverflowTutorial 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.
Top answer 1 of 5
102
var declaration is function scoped and let declaration is block scoped.
See https://basarat.gitbook.io/typescript/future-javascript/let for more details.
2 of 5
45
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
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
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
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
Videos
08:48
var, let, and const: What is the Big Difference! - YouTube
04:15
Var vs Let vs Const: Choosing the Right Variable in JavaScrip - ...
03:57
var vs let in TypeScript (With Examples) - YouTube
04:27
var, const, let... now ‘using’? - YouTube
01:35
What's the difference between var, let, and const? | JavaScript ...
r/ProgrammerHumor on Reddit: me before I learned the difference ...
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.
Reddit
reddit.com › r/typescript › var, const, let
r/typescript on Reddit: var, const, let
April 24, 2018 -
I confuse the following term var, const, let in typeScript and when should I use them?
Top answer 1 of 4
34
Rule of thumb: start with const, if you need to change the value later, change to let, and forget about var.
2 of 4
17
let - use this when you are declaring a variable to be used in a scope and possibly re-assigned later. const - use this when you know you never need to re-assign to the variable. assign when you declare and never again. var - don't use this. it's old and pointless. That's it!
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 › 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.
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.
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.
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.
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.