Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
What are the differences between let, const, and var in JavaScript?
Asked on Feb 20, 2026
Answer
In JavaScript, "let", "const", and "var" are used to declare variables, but they have different behaviors regarding scope, hoisting, and reassignment. Here's a concise example to illustrate these differences:
// var example
function varExample() {
if (true) {
var x = 10;
}
console.log(x); // 10, var is function-scoped
}
// let example
function letExample() {
if (true) {
let y = 20;
}
// console.log(y); // Error, let is block-scoped
}
// const example
function constExample() {
const z = 30;
// z = 40; // Error, const cannot be reassigned
}Additional Comment:
✅ Answered with JavaScript best practices.- "var" is function-scoped and can be redeclared and updated.
- "let" is block-scoped and can be updated but not redeclared in the same scope.
- "const" is block-scoped and cannot be updated or redeclared; it must be initialized at the time of declaration.
- "var" is hoisted to the top of its function scope, while "let" and "const" are hoisted to the top of their block scope but are not initialized.
Recommended Links:
