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 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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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
var
within anif
, for example, if that's the first place it is used, as you can in python, but I think usingvar
insidefor
, for example, opens you up to weird bugs.I agree that in principle it is possible to use
var
responsibly. 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.