DEV Community

Discussion on: Is`let` the new `var` in Javascript?

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.