let, const, and var
the use case of let, const and var; avoiding errors, such as the temporal dead zone, and using a linter
Rule 1: use
let
for variables, andconst
for constants whenever possible. Usevar
only when you have to maintain legacy code.
The following rule is also worth keeping.
Rule 2: Always declare and initialize all your variables at the beginning of your scope.
Using rule 2 implies that you never have to face with the temporal dead zone.
If you have a linter such as ESLint, set it up accordingly, so that it warns you when you violate the second rule.
If you stick to these two rules, you will get rid of most of the anomalies developers face.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.