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 19, 2026
Answer
In JavaScript, "var", "let", and "const" are used to declare variables, but they have different scopes and characteristics. Here's a concise explanation with a code example:
// 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 or globally-scoped and allows re-declaration within the same scope.
- "let" is block-scoped, meaning it is limited to the block where it is defined, and does not allow re-declaration in the same scope.
- "const" is also block-scoped and is used to declare constants, meaning the value cannot be reassigned after the initial assignment.
Recommended Links:
