Recently I was reading about a debate on var vs let in Javascript. The debate started over the sentence: let is the new var.
I personally disagree with the statement. I think they both have their own use cases. But I do not think that there is need to replace function level variable declarations with let.
What do you guys think?
Oldest comments (13)
I think
varshould be used sparingly. Because of hoisting, you can be left in a state where you've accidentally put a value into a variable before you initialise it. Alsovarallows you to declare your variable multiple times, so perhaps, if you're forgetful like me, you'll accidentally use the same variable name for different things and end up confused as to why the variable has the wrong value.Using
letprevents all that.I'm a firm believer in the practice of declaring variables as close as possible to where you will start using them.
letallows that,varsecretly prevents it.I also like to define the variables close to where they are being used. But I believe that if we declare the variables using
var, we communicate that this variable is being used in the whole function body. I think it might be helpful to the code reader.I can see how it could be useful to declare a
varwithin anif, for example, if that's the first place it is used, as you can in python, but I think usingvarinsidefor, for example, opens you up to weird bugs.I agree that in principle it is possible to use
varresponsibly. I still preferlet, as I know it'll be gone when my scope changes, so I don't have to think about it any more.Although as Ellen says, using
letcould prevent re-declaration of variables, using it can cause issues in older browsers like Internet Explorer. Personally I will usevarinstead and I will be pretty careful about the variable names.The variable name I set is as less generic as possible to reduce the possibility to be re-declared.
You use var inside blocks (like
if-elseand loops) also? There I useletbecause it will prevent accidental value changes in the variable.constshould always be used when you don’t need to change the value, and if that’s not possible,letshould be used, but notvar.There are some edge cases where
varis the only option, but you should avoid that.I don't use either of them hardly at all unless I need to support a browser that only recognizes
var. If I need to declare a value procedurally, I useconst, but in most of my code I try to closely adhere to a stateless, functional style that evaluates instead of executes. Procedural code has caused me too many bugs. Highly-constrained code frees me to focus on the pure logic of my app.Personally I used
constwherever possible. I useletwhere I need mutability, but find that its often a sign that I need to rethink what I'm doing, and I pretty much pretend thatvardoesn't exist.Both hoisting and the fact that
varis function scoped rather than block scoped adds a lot of unnecessary complexity to reasoning about my code.letandconstmake code much less ambiguous.I've been seriously coding in JS pretty much since ES6 became mainstream and I have not once found a time when I needed to use
varto do anything I needed to do.As far as you understand the difference between var, let and const you are free to use any of those. The issue comes in when you are working with a team.
While many would understand how hoisting works in javascript, a lot of the people coming from other languages do not.
varis certainly not easy to digest specially when combined with function declarations and loops.In that case you would like the team to follow certain guidelines. You can restrict the use of
varand it should work. There is no performance penalty and in addition you get the power of block scoping and also making it easier for others to write programs.Completely agree here. It highly depends on the team and the style of writing code. But I also believe that using
var(that this variable is used throughout the function) orlet(that this variable is used only in the current block) carries a special meaning. And you are correct. Concepts ofvarare not easy to digest.well, "you are free to use any of those" it's not that "as is" at all. If you use const and you don't know how consts works you'll get stuck trying to imagine why this fkin const is not changing their value even set on a lefthand just before. 😆
Yeah, of course - been there:-D. But I guess, engines report the re-assignment error properly. At least on browser you get -
Uncaught TypeError: Assignment to constant variable.And I did start with "as far as you understand the difference", If you don't then you can always follow what others are doing. Useleteverywhere.I'd say
constis the new var.var was the default (and only) option to declare a variable, and
constI would say is the new "default".