Var, let, and const β Once and For All! π
β
JavaScript Variables Cheat Sheet β var vs let vs const (With Visual)
If you're a JavaScript developer, knowing the difference between var, let, and const isn't optional β it's foundational.
Hereβs a quick cheat sheet to help you write cleaner and bug-free code π§Όπ
π Quick Comparison
| Keyword | Scope | Hoisted | TDZ | Reassign? | Redeclare? |
var | Function | β Yes (undefined) | β No | β Yes | β Yes |
let | Block | β Yes | β Yes | β Yes | β No |
const | Block | β Yes | β Yes | β No | β No |
πQuick Comparison
var
πΉ Scope: Function
πΉ Hoisted: Yes (initialized to
undefined)πΉ TDZ: β No Temporal Dead Zone
πΉ Reassignable: β Yes
πΉ Redeclarable: β Yes
let
πΉ Scope: Block
πΉ Hoisted: Yes
πΉ TDZ: β Yes
πΉ Reassignable: β Yes
πΉ Redeclarable: β No
const
πΉ Scope: Block
πΉ Hoisted: Yes
πΉ TDZ: β Yes
πΉ Reassignable: β No
πΉ Redeclarable: β No
π§ Key Takeaways
Use
constby default (safer and predictable).Use
letonly when reassignment is required.Avoid
varin modern JavaScript.constdoesn't mean immutable β objects/arrays can still be changed.
π¬ Whatβs your go-to tip when teaching or using JavaScript variables?
Letβs help each other write smarter code. Drop your favorite JS trick below π
#JavaScript #WebDevelopment #Frontend #CodeTips #DeveloperTools #CleanCode #CheatSheet