What Is the Difference Between var, let, and const?
Understand the differences between var, let, and const in JavaScript, including scope, hoisting, and best practices.
What Is the Difference Between var, let, and const?
JavaScript provides three ways to declare variables:
- var
- let
- const
Understanding their differences is important for writing reliable code.
var
Variables declared with var:
- Are Function Scoped
- Can Be Redeclared
- Can Be Reassigned
- Are Hoisted
Example:
var name = "John"; var name = "Jane";
The redeclaration is allowed.
let
Variables declared with let:
- Are Block Scoped
- Cannot Be Redeclared In The Same Scope
- Can Be Reassigned
Example:
let age = 25; age = 26;
Reassignment is allowed.
const
Variables declared with const:
- Are Block Scoped
- Cannot Be Redeclared
- Cannot Be Reassigned
Example:
const PI = 3.14;
The variable reference cannot be changed.
Scope Comparison
Block scope means variables only exist inside the block where they are declared.
This behavior helps prevent accidental bugs.
Which One Should You Use?
A common recommendation is:
- Use const by default.
- Use let when reassignment is required.
- Avoid var in modern JavaScript.
The Bottom Line
let and const are generally preferred over var because they provide block scope and help developers write safer, more predictable code.
In modern JavaScript, let and const are generally preferred over var.
The variable reference cannot be reassigned, although object contents may still be modified.
No. var is function scoped.
No. let cannot be redeclared within the same scope.
var can lead to unexpected behavior due to function scope and hoisting.
Ready to master Javascript completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Javascript
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

