Web Development · JavaScript

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

Beginner
~3 min answer
0 views
0 likes
2026
Quick Answer

var is function-scoped, let and const are block-scoped. const cannot be reassigned, let can.

Detailed Answer
var, let, and const are three ways to declare variables in JavaScript:

var:
- Function-scoped
- Can be re-declared and updated
- Hoisted to top of function with undefined

let:
- Block-scoped ({})
- Cannot be re-declared but can be updated
- Not hoisted (TDZ - Temporal Dead Zone)

const:
- Block-scoped
- Cannot be re-declared or updated
- Must be initialized at declaration
- Objects/arrays can still be mutated
Example / Code
let count = 0; count++; // OK
const name = "ADN"; name = "test"; // Error!
Interview Tips

Always use const by default, use let when you need to reassign, avoid var in modern JS.

Common Mistakes

Thinking const makes objects completely immutable. Forgetting about temporal dead zone with let.

Related Skills
ES6 React Node.js