I am sorry if this is asked already but as I am introducing myself in ES6 on some tutorials, the instructor is saying that the var statement should be avoided. It just feels wrong but, is there any good reason for why should it be avoided?
Asked
Active
Viewed 2,356 times
3
-
what is the alternative they suggest? – sova Sep 25 '16 at 00:46
-
2That is an over-broad statement. It's a matter of opinion, but it seems like an unnecessary "rule". – Pointy Sep 25 '16 at 00:46
-
@sova I presume it's to always use `let`. – Pointy Sep 25 '16 at 00:47
-
1Did you read ***[“let” keyword vs “var” keyword in Javascript](http://stackoverflow.com/q/762011/560648)***? It was presented to you as a possible duplicate as you typed this question. – Lightness Races in Orbit Sep 25 '16 at 00:47
-
@LightnessRacesinOrbit thank you for the informative link. It was not suggested, my question statement is slightly different – learningjavascriptks Sep 25 '16 at 00:50
-
_"the instructor is saying"_ Have they written that down as part of syllabus or other material distributed to class? And provided reason why they believe this? – guest271314 Sep 25 '16 at 01:00
-
@guest271314 no hoisting takes place on 'let' unlike var, and that let is doing away with certain type of bugs and that developers are using let already and doing away with 'var'. It just felt wrong for me that's why I asked – learningjavascriptks Sep 25 '16 at 01:09
-
It will be regarded as opinion, and wrong, by many. 'let' and 'var' do different things: avoid using `let` for a loop variable you want to use after the loop exits, but one way of [capturing a loop counter](http://stackoverflow.com/questions/30899612/explanation-of-let-and-block-scoping-with-for-loops) in a closure is to use `let` for the counter variable. My _opinion_ is that `let` has become trendier than it deserves. – traktor Sep 25 '16 at 02:35
-
I use var when I have no specific need of let. It was here before and has most backward compatibility, even if we don't need the compatibility anymore. It is like DOM API, a DOM1 property like nodeValue is fine unless you really need a DOM3 property like textContent. See also: https://www.quora.com/Is-%E2%80%98let%E2%80%99-or-%E2%80%98var%E2%80%99-more-appropriate-for-declaring-variables-in-JavaScript/answer/Glenn-Anderson?ch=10&oid=146277096&share=3e76ca68&target_type=answer – baptx Nov 14 '22 at 22:45
1 Answers
2
var is on ES6 for legacy reasons. In theory, the let statement is better since it behaves more predictably on block scopes, but it won't work with more outdated interpreters. So, if you're coding with only ES6 in mind, go for let.
EDIT: Also, keep in mind that if you're new to JS, you'll likely find more learning material using var, so keep that in mind.

Rafael
- 557
- 6
- 21
-
5Source, for the legacy reason statement? If not, I don't think you should spread this. – Ryan Sep 25 '16 at 00:55
-
2There's nothing really *wrong* with `var`. If you really do want function-scoped variables (a very common thing), and you're declaring the variables C-style at the top of the function, then there's no reason not to use `var` and (personal opinion) it's clearer and more explicit than `let`. – Pointy Sep 25 '16 at 01:05