Most programmers says that "let" should be always used, so here comes the question: Is there any reason to still use var or it is just completely "deprecated"?
-
You can do some cute tricks with hoisting, but as a general rule, never use `var` unless you know exactly why you're using it. – John Montgomery Aug 14 '18 at 01:07
-
1I would say the opposite. As a general rule use `var` because it doesn't break compatibility. If you know what you are doing then use let. – Ibu Aug 14 '18 at 01:08
-
[How to fix this ES6 module circular dependency?](https://stackoverflow.com/a/42704874/4642212) may be one case. – Sebastian Simon Aug 14 '18 at 01:10
-
2I would add the following: As a general rule, only use `var` or `let` if you know exactly what you are doing =) – Mathiasfc Aug 14 '18 at 01:13
-
**let** variables are usually used when there is a limited use of those variables. For example, an unsynchronized data could be modified during waiting time. If you use **let** it could lock your data. This is good for you. [Difference between let and var in Javascript](https://codeburst.io/difference-between-let-and-var-in-javascript-537410b2d707) – Phineas Huang Aug 14 '18 at 01:23
-
@lbu I disagree. If you default to using *var* you will spend half your time debugging your code trying to understand why *x* = *y* when you haven't assigned *x* in the current function. It's easier to debug an error (*an unassigned variable error*), than it is unexpected "added" functionality. – NotoriousPyro Aug 14 '18 at 08:24
-
@Ibu I disagree too. You are saying yourself: "If you know what you are doing then use **let**": it means that you may keep using **var** when you don't know what you are doing. I have this problem everyday with my students. Using **var** opens the door to bad coding, yielding unstable code. – allez l'OM Dec 02 '19 at 10:04
1 Answers
let should be used if the variable is only used in the current scope, or should not be shared outside the current scope such as if the same name is used elsewhere but should not be set before reuse. It is useful for mass assigning variables to make your code more readable and easier to write inside the scope.
For example you might want to write
for (let city of cities) {
console.log(city.name + city.state + city.areacode);
}
But you might want to not have to write city. each time - because although this for loop doesn't do too much now - in the future you want a lot more in it! So in this case you'd do:
for (let city of cities) {
let cname = city.name;
let cstate = city.state;
let carea = city.areacode;
console.log(cname + cstate + carea);
}
and this allows you to separate the data from the logic a bit better... without having to worry about cname being set elsewhere.
In terms of scope of the variable:
people = ["Frank", "Tina"];
for (let p of people) {
console.log(p);
}
console.log(p);
This would throw an unassigned variable error but after showing the first two names.
var is extremely useful if you want to assign a variable scope that everything can see. It is useful for say, assigning a url to retrieve json data, and then changing that url on the fly... So all your other functions always access the same data, rather than calling a function to get it each time. They're useful if you maybe want to track an element, object and the like over several different functions.
people = ["Frank", "Tina"];
for (var p of people) {
console.log(p);
}
console.log(p);
This would show both names, but the last name twice.
const is like var but cannot be changed once assigned.

- 526
- 3
- 16
-
-
-
1Your example with **var** demonstrates exactly the opposite: if you want _p_ to be in the external scope, you must declare it in the external scope. If you don't do that, you are preparing yourself, or users of your code, to forthcoming problems ... precisely what people are reproaching to JavaScript. Banning the use of **var** (with new code) is the solution. – allez l'OM Dec 02 '19 at 09:54