let
is introduced in Ecma Script 6 - the new javascript version - which is still in development at the time of this writing. Therefore, using var
will get you across more browsers for the time being.
On the other hand, I urge people to use let
instead of var
from now on. I would go ahead and list the reasons but you have already have a link in your post with a great explanation in it. Long story short, let
helps you avoid the variable hoisting in javascript and keep your variables' scope at just where it needs to be.
update
I've forgotten about this post until I recently got an upvote. I'd like to update this statement. I personally use const
as much as I can these days. If you get into linting your code, which I highly recommend, and use something like airbnb lint rules, it will also tell you to use const. It will make your variables a constant so you will not be able to mutate them once you set their value thus it is not applicable in all cases. const
and let
also have advantages in terms of scope over var
and it is well worth a read. My hierarchy of usage goes as such const
> let
> var
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…