In JavaScript, var, let, and const are used to declare variables. However, they differ in terms of scope, hoisting, reassignability, and block-level usage. Understanding these differences is crucial for writing clean, efficient, and bug-free code.
var was the original way to declare variables in JavaScript before ES6.
- Scope: Function-scoped.
- A variable declared with
varis accessible within the function it is defined in, but not outside of it.
- A variable declared with
- Hoisting: Variables declared with
varare hoisted to the top of their scope and initialized asundefined. - Reassignment: Variables declared with
varcan be re-assigned and re-declared. - Block-Level Behavior:
varis not block-scoped; it ignores block-level{}braces and behaves as though the block doesn't exist.
function example() {
if (true) {
var x = 10;
}
console.log(x); // Output: 10 (accessible outside the block)
}
example();
console.log(y); // Output: undefined (hoisted)
var y = 20;let was introduced in ES6 as an improvement over var.
- Scope: Block-scoped.
- Variables declared with
letare accessible only within the block they are defined in.
- Variables declared with
- Hoisting: Variables declared with
letare hoisted but are not initialized, meaning they cannot be accessed before their declaration (Temporal Dead Zone). - Reassignment: Variables declared with
letcan be re-assigned but not re-declared within the same scope.
function example() {
if (true) {
let x = 10;
console.log(x); // Output: 10
}
// console.log(x); // Error: x is not defined (block-scoped)
}
example();
let y = 20;
// let y = 30; // Error: Identifier 'y' has already been declared
y = 30; // Reassignment worksconst was also introduced in ES6 and is used for variables that should not be re-assigned.
- Scope: Block-scoped.
- Similar to
let,constis accessible only within the block it is defined in.
- Similar to
- Hoisting: Variables declared with
constare hoisted but not initialized, leading to a Temporal Dead Zone. - Reassignment: Variables declared with
constcannot be re-assigned or re-declared. - Constant Reference: If a
constvariable holds an object or array, its properties or elements can still be modified becauseconstprevents reassignment of the reference, not the content.
const x = 10;
// x = 20; // Error: Assignment to constant variable
const obj = { name: "John" };
obj.name = "Doe"; // This works (modifying object properties)
console.log(obj); // Output: { name: "Doe" }
// const y; // Error: Missing initializer in const declaration| Feature | var |
let |
const |
|---|---|---|---|
| Scope | Function-scoped | Block-scoped | Block-scoped |
| Hoisting | Hoisted and initialized | Hoisted but uninitialized | Hoisted but uninitialized |
| Reassignment | Allowed | Allowed | Not allowed |
| Re-declaration | Allowed | Not allowed (same scope) | Not allowed (same scope) |
| Block-Level Usage | Not block-scoped | Block-scoped | Block-scoped |
- Use
varonly when you need to support very old browsers or specific legacy cases. - Use
letfor variables that may change value. - Use
constfor variables that should not be reassigned (e.g., constants or fixed references).
By understanding the differences, you can choose the appropriate variable declaration method for your needs, making your code cleaner and more predictable.