What’s the difference between var, let, and const?
Sound's simple on paper yet this question still trips experienced developers.
JavaScript gives us three ways to declare variables. They seem interchangeable until one of them ruins your evening and sends you debugging at 2 AM.
In this guide, we’ll clearly break down how var, let, and const work — and when to use each.

The Chaotic Era of var
Before ES6 (2015), JavaScript only had var which let to problems such as:
- accidental global variables
- scope confusion
- variables being overwritten unintentionally
To fix these issues, ES6 introduced two variables named:
- let -> stays inside its block and minds its own business.
- const -> locked in place like your ATM pin.
1.Var (Old School)
Before ES6, var was the default setting for every variable. It didn’t care about block boundaries, happily leaked into places it shouldn’t, and occasionally created bugs that made developers question their life choices.
How var Works:
- You can declare it again without errors
- It does NOT stay inside blocks like if or for
- Exists throughout the entire function it’s declared in
- You can use it before it appears in the code (it will start as undefined)
var name = "Clark Kent"
var name = "Definitely not Superman"
console.log(name) // prints Definitely not Superman
Because var is function scoped, it ignores all bondaries like a boy bestie.
if(true){
var message = "Message Works"
}
console.log(message) // prints "message works, not checking any boundaries"
2.Let (The well behaved guest)
let was introduced in ES6 to fix the scope problems caused by var. A variable declared with let is block scoped, meaning it only exists inside the { } where it is defined.
This makes your code more predictable and prevents variables from leaking into places they shouldn’t be.
You can change the value later, but you cannot declare it again in the same scope.
Think of let as a well-behaved variable, it stays in its lane and only shows up where it’s invited.
How let Works:
- Must be declared before it is used
- Cannot be accessed outside its block
- Helps prevent accidental variable leaks
- Works well in loops and conditional blocks
- Exists only inside the block { } where it is declared
let deadline = "Next Week"
let deadline = "Tonight" // Fails to print since let is declared two times.
But you can redeclare the values like this
let sleephours = 8
sleephours = 4
console.log(sleephours) // Prints 4
Var only works inside the block. Outside the block it acts like it never existed
if(true){
let work ="Done"
}
console.log(work); // Error not accessible outside its block.
Const (The School Principal)
const was introduced in ES6 to declare variables whose values should not be reassigned.
Like let, it is block scoped, meaning it only exists inside the { } where it is defined. Once a value is assigned to a const variable, it cannot be changed or redeclared.
This helps prevent accidental changes and makes your code safer and easier to understand.
Think of const as a promise: once set, it stays that way.
How const Works:
- Cannot be reassigned later
- Prevents accidental value changes
- Cannot be redeclared in the same scope
- Must be assigned a value at the time of declaration
- Exists only inside the block { } where it is declared
Once const variable is declared we cannot reassign the values
const pi = 3.14
pi = 365
console.log(pi)// Error cannot redeclare the values once const is used.
But objects can change it's value internally
const diet ={ start: "Monday" };
diet.start = "next Monday"; // Prints next Monday
console.log(diet);
As let it is also block scoped
if(true){
let work ="Done"
}
console.log(work); // Error not accessible outside its block.
When you should use Each?
- Use const whenever the value should not change.
- Use let when you expect the value to update later.
- var is mostly kept for legacy code (Caution: Do Not Touch).
A Short Recap
- const is your date of birth.
- let is your mood.
- var is your weekend plans.
Final Thoughts
JavaScript seems confusing… until you understand it and start questioning your life choices.
If you enjoyed this quick guide, you can read more developer-friendly tips on my blog. I regularly share insights on coding, productivity, and building better projects.
Let’s connect — follow me on Twitter and LinkedIn so you don’t miss future posts.