var, let and const in javascript
CC
Coderschain TeamJanuary 24, 2026
5 min read

Understanding var, let, and const in JavaScript
JavaScript provides three ways to declare variables: var, let, and const.
Though they may look similar, they behave very differently. Understanding these differences helps you write cleaner and more predictable code.
1. var
var was the original way to declare variables in JavaScript.
Characteristics
- Function-scoped
- Can be redeclared
- Can be reassigned
- Hoisted and initialized as
undefined
Example
var name = "Alice";
var name = "Bob";
name = "Charlie";
console.log(name);
Scope Issue
if (true) {
var x = 10;
}
console.log(x); // 10
2. let
let was introduced in ES6 and is block-scoped.
Characteristics
- Block-scoped
- Cannot be redeclared in the same scope
- Can be reassigned
- Hoisted but not initialized (Temporal Dead Zone)
Example
let age = 25;
age = 26;
// let age = 30; // Error
Block Scope
if (true) {
let y = 20;
}
// console.log(y); // ReferenceError
3. const
const is used for values that should not be reassigned.
Characteristics
- Block-scoped
- Cannot be redeclared
- Cannot be reassigned
- Must be initialized when declared
Example
const PI = 3.14;
// PI = 3.14159; // Error
Objects and Arrays
const user = { name: "John" };
user.name = "Jane";
const numbers = [1, 2, 3];
numbers.push(4);
4. Comparison Table
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Redeclare | Yes | No | No |
| Reassign | Yes | Yes | No |
| Hoisting | Yes | TDZ | TDZ |
5. Best Practices
- Use
constby default - Use
letwhen reassignment is needed - Avoid
varin modern JavaScript
Example
const appName = "My App";
let counter = 0;
counter++;
Conclusion
varis outdated and error-proneletis safer for mutable valuesconstis best for fixed references
