Programming Languages JavaScript Subjective
Sep 25, 2025

What is the difference between var, let, and const?

Detailed Explanation

Key differences:

var:
- Function-scoped or globally-scoped
- Can be redeclared and updated
- Hoisted and initialized with undefined

let:
- Block-scoped
- Can be updated but not redeclared in same scope
- Hoisted but not initialized (temporal dead zone)

const:
- Block-scoped
- Cannot be updated or redeclared
- Must be initialized at declaration
- Hoisted but not initialized (temporal dead zone)

Example:
function example() {
var a = 1;
let b = 2;
const c = 3;

if (true) {
var a = 4; // Same variable
let b = 5; // Different variable
// const c = 6; // Error: already declared
}
}

Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback