DEV Community

Cover image for Is`let` the new `var` in Javascript?
Amit Khonde
Amit Khonde

Posted on

Is`let` the new `var` in Javascript?

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)

Collapse
 
mellen profile image
Matt Ellen • Edited

I think var should 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. Also var allows 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 let prevents all that.

I'm a firm believer in the practice of declaring variables as close as possible to where you will start using them. let allows that, var secretly prevents it.

Collapse
 
amitkhonde profile image
Amit Khonde

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.

Collapse
 
mellen profile image
Matt Ellen • Edited

I can see how it could be useful to declare a var within an if, for example, if that's the first place it is used, as you can in python, but I think using var inside for, for example, opens you up to weird bugs.

I agree that in principle it is possible to use var responsibly. I still prefer let, as I know it'll be gone when my scope changes, so I don't have to think about it any more.

Collapse
 
littlephone profile image
littlephone • Edited

Although as Ellen says, using let could prevent re-declaration of variables, using it can cause issues in older browsers like Internet Explorer. Personally I will use var instead 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.

Collapse
 
amitkhonde profile image
Amit Khonde

You use var inside blocks (like if-else and loops) also? There I use let because it will prevent accidental value changes in the variable.

Collapse
 
oskarcodes profile image
Oskar Codes

const should always be used when you don’t need to change the value, and if that’s not possible, let should be used, but not var.

There are some edge cases where var is the only option, but you should avoid that.

Collapse
 
webbureaucrat profile image
webbureaucrat

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 use const, 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.

Collapse
 
rubyrubenstahl profile image
Ruby Rubenstahl

Personally I used const wherever possible. I use let where I need mutability, but find that its often a sign that I need to rethink what I'm doing, and I pretty much pretend that var doesn't exist.

Both hoisting and the fact that var is function scoped rather than block scoped adds a lot of unnecessary complexity to reasoning about my code. let and const make 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 var to do anything I needed to do.

Collapse
 
amt8u profile image
amt8u

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. var is 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 var and 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.

Collapse
 
amitkhonde profile image
Amit Khonde

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) or let (that this variable is used only in the current block) carries a special meaning. And you are correct. Concepts of var are not easy to digest.

Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡

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. πŸ˜†

Collapse
 
amt8u profile image
amt8u

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. Use let everywhere.

Collapse
 
webd00d profile image
Christian Bryant

I'd say const is the new var.

var was the default (and only) option to declare a variable, and const I would say is the new "default".