Var vs Let vs Const Summary
You'll learn how var, let, and const differ in scope, hoisting behavior, and reassignment rules—so you can choose the right one every time.
The Big Picture
Think of these three keywords as different types of containers for your values. Each has its own rules about where it can be accessed (scope), how it's created (hoisting), and whether you can change what's inside (reassignment). Understanding these differences helps you write more predictable, bug-free code.
The Key Differences
Scope
- var: Function-scoped (or global if outside functions)
- let: Block-scoped (limited to
{}curly braces) - const: Block-scoped (limited to
{}curly braces)
Hoisting
- var: Hoisted and initialized to
undefined - let: Hoisted but not initialized (Temporal Dead Zone)
- const: Hoisted but not initialized (Temporal Dead Zone)
Reassignment
- var: Can be reassigned
- let: Can be reassigned
- const: Cannot be reassigned (but objects/arrays can be mutated)
Quick Analogy
Think of var as an old filing cabinet—it's flexible but messy, documents can pop up in unexpected drawers. Let is a labeled drawer that only opens in the right room. Const is a locked drawer—you can't swap out what's inside, but if it's a container of items, you can still rearrange those items.
Modern Best Practice
Use const by default for values you won't reassign. Use let when you need to change the variable's value. Avoid var in modern JavaScript—it exists mainly for legacy code compatibility.
Key Takeaway: const and let are block-scoped and safer; var is function-scoped and outdated. Start with const, switch to let only when reassignment is needed.