Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
What are the differences between var, let, and const in JavaScript?
Asked on May 29, 2026
Answer
In JavaScript, "var", "let", and "const" are used to declare variables, but they differ in terms of scope, hoisting, and mutability. Here's a concise comparison:
// var example
var x = 1;
if (true) {
var x = 2; // same variable
console.log(x); // 2
}
console.log(x); // 2
// let example
let y = 1;
if (true) {
let y = 2; // different variable
console.log(y); // 2
}
console.log(y); // 1
// const example
const z = 1;
// z = 2; // Error: Assignment to constant variable
console.log(z); // 1Additional Comment:
✅ Answered with JavaScript best practices.- "var" is function-scoped and can be redeclared or updated within its scope. It is hoisted to the top of its scope.
- "let" is block-scoped, meaning it is limited to the block in which it is defined. It can be updated but not redeclared within the same scope.
- "const" is also block-scoped like "let", but it cannot be updated or redeclared. The value must be initialized at the time of declaration.
- Use "let" and "const" for block-level scoping to avoid issues with "var" hoisting and redeclaration.
Recommended Links:
