Skip to main content

Command Palette

Search for a command to run...

Var, let, and const β€” Once and For All! πŸš€

Updated
β€’2 min read

βœ… 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

KeywordScopeHoistedTDZReassign?Redeclare?
varFunctionβœ… Yes (undefined)❌ Noβœ… Yesβœ… Yes
letBlockβœ… Yesβœ… Yesβœ… Yes❌ No
constBlockβœ… 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 const by default (safer and predictable).

  • Use let only when reassignment is required.

  • Avoid var in modern JavaScript.

  • const doesn'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