var vs let vs const

The three ways to declare variables in JavaScript. Understanding their differences in scope, hoisting, and reassignment is fundamental to writing modern JS.

Featurevarletconst
ScopeFunction-scopedBlock-scopedBlock-scoped
HoistingHoisted and initialized to undefinedHoisted but not initialized (TDZ)Hoisted but not initialized (TDZ)
When to useNever (legacy code only)When the value needs to changeDefault choice for all declarations
ReassignmentYesYesNo
RedeclarationYes (in same scope)No (in same scope)No (in same scope)
Temporal Dead ZoneNoYesYes

Verdict

Use const by default. Use let only when you need to reassign. Never use var in modern code.

Code Example

Javascript
Tip: Modify the code above and click “Run” to see the results

Related Tutorials

Related Glossary Terms