Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
What is the difference between var, let, and const in JavaScript? Pending Review
Asked on May 05, 2026
Answer
In JavaScript, "var", "let", and "const" are used to declare variables, but they differ in terms of scope, hoisting, and mutability.
// var example
var x = 10;
if (true) {
var x = 20; // same variable
console.log(x); // 20
}
console.log(x); // 20
// let example
let y = 10;
if (true) {
let y = 20; // different variable
console.log(y); // 20
}
console.log(y); // 10
// const example
const z = 10;
// z = 20; // Error: Assignment to constant variableAdditional Comment:
✅ Answered with JavaScript best practices.- "var" is function-scoped and can be redeclared or updated. It is hoisted to the top of its scope.
- "let" is block-scoped, cannot be redeclared within the same scope, but can be updated.
- "const" is block-scoped and cannot be redeclared or updated. It must be initialized at the time of declaration.
- Use "let" and "const" for block-level scope to avoid issues with "var" hoisting.
Recommended Links:
