Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
What are the differences between `let`, `var`, and `const` in JavaScript?
Asked on Feb 11, 2026
Answer
In JavaScript, "let", "var", and "const" are used to declare variables, but they have different behaviors in terms of scope, hoisting, and mutability.
// Example of var
var name = "Alice";
if (true) {
var name = "Bob"; // Re-declares and changes the same variable
}
console.log(name); // Outputs: Bob
// Example of let
let age = 30;
if (true) {
let age = 25; // Creates a new variable in the block scope
}
console.log(age); // Outputs: 30
// Example of const
const birthYear = 1990;
// birthYear = 1991; // Error: Assignment to constant variableAdditional Comment:
✅ Answered with JavaScript best practices.- "var" is function-scoped or globally-scoped and can be re-declared and updated.
- "let" is block-scoped and can be updated but not re-declared within the same scope.
- "const" is block-scoped and cannot be updated or re-declared. It must be initialized at the time of declaration.
- "var" is hoisted to the top of its scope and initialized with "undefined", while "let" and "const" are hoisted but not initialized.
Recommended Links:
