In JavaScript, var
, let
, and const
are used to declare variables—but they differ in how they behave when it comes to scope, hoisting, and mutability. Choosing the right one helps prevent bugs and makes your code easier to reason about.
In modern development, let
and const
are preferred. var
is mostly reserved for legacy code or situations requiring function-scoped variables. Many developers search “what is let var const” — this guide breaks it all down so you can write cleaner, safer JavaScript.
What’s the difference between var, let, and const?
Understanding scope in JavaScript
Variable scope determines where a variable is accessible.
var
is function-scoped — accessible throughout the entire function.let
andconst
are block-scoped — accessible only within the{ }
block they are defined in.
function example() {
if (true) {
var x = 1;
let y = 2;
}
console.log(x); // ✅ Outputs 1
console.log(y); // ❌ ReferenceError
}
What do hoisting, reassignable, and redeclarable mean?
Hoisting
JavaScript moves variable declarations to the top of their scope.
- With
var
the variable is hoisted and initialized asundefined
. - With
let
andconst
the declaration is hoisted but not initialized, so using them before their declaration results in aReferenceError
.
Reassignable
Can you assign a new value to the variable after declaring it?
let
andvar
allow reassignment.const
does not.
Redeclarable
Can you declare the same variable name again in the same scope?
var
allows it.let
andconst
do not — which helps prevent accidental bugs.
How to choose between var, let, and const
- ✅ Use
const
by default. - Prevents accidental reassignment and communicates intent clearly.
- 🔁 Use
let
when the variable will change. - Useful in loops, state changes, toggles, and accumulations.
- 🚫 Avoid
var
unless maintaining legacy code var
still works, but its function scope and redeclaration behavior make it error-prone in modern codebases.
When to use var, let and const
✅ Use const
when:
- The variable should never be reassigned.
- You want stable, predictable references.
- You're working with arrays or objects that can be mutated but shouldn’t be reassigned entirely.
const config = { debug: false };
config.debug = true; // ✅
allowedconfig = {}; // ❌ Error: Assignment to constant variable
🔁 Use let
when:
- You need to reassign the variable later.
- You're using a variable inside a block (
for
,if
, etc.). - You want to avoid accidental redeclaration.
let score = 0;
score += 10;
🕰 Use var
when:
- You're working in legacy ES5 codebases.
- You need function-level scope instead of block scope.
- You’re supporting very old browsers.
function legacyCode() {
if (true) {
var total = 42;
}
console.log(total); // ✅ 42 (still accessible)
}
FAQ
Is it better to use let or const?
Use const
whenever possible. It helps prevent bugs by signaling that the variable won’t change. Use let
when reassignment is necessary.
Why is var discouraged in modern JavaScript?
var
is function-scoped and allows redeclaration, which can lead to confusing bugs. let
and const
offer safer scoping and behavior.
Is let faster than var?
Not really. Performance is nearly identical in most cases. The real benefit is developer clarity and fewer bugs, not execution speed.
Can you reassign a const variable?
No — but you can change the contents of arrays or objects declared with const
.
const colors = ['red', 'blue'];
colors.push('green'); // ✅ Works
colors = ['yellow']; // ❌ Error
What happens if you use var inside a function?
It’s hoisted and scoped to the entire function, initialized as undefined
no matter where you declare it.
Does let have block scope?
Yes. let
only exists within the {}
block where it's declared.
Can you redeclare variables with let
or const
?
No — attempting to redeclare in the same scope throws a SyntaxError
.
let a = 1;
let a = 2; // ❌ Error
Want to learn more JavaScript?
Learn to code using Scrimba with their interactive follow-along code editor.
Join their exclusive discord communities and network to find your first job!
Use our affiliate link for a 20% discount!!
- Click the link to take you to the Scrimba site
- A pop-up should appear on your screen with the discount amount and instructions on how to claim it
- Discount is for new accounts only
We receive a monetary kickback if you use our affiliate link and make a purchase.