Should you use var or let

Hi,

I have been studying JavaScript and while reading the PDF Eloquent JavaScript it mentions how you should use let instead of var. I noticed everyone that teaches JavaScript is always using var.

Am i missing something?

You should use let or const. Var is older syntax which still works, but is subpar to let/const. Usually when you see tutorials using var it is either because they are old or because the creators havn’t made the transition to ES6 yet.

2 Likes

It is preferred to use const/let over var because block scoping gives benefits over function scope.
It is also preferred to use const over let, as that helps to make the code more reliable.

1 Like