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?
Asked on May 25, 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 explanation with a code example:
// var: Function-scoped or globally-scoped, can be re-declared and updated
var x = 10;
var x = 20; // Re-declaration is allowed
// let: Block-scoped, cannot be re-declared in the same scope, but can be updated
let y = 10;
y = 20; // Update is allowed
// const: Block-scoped, cannot be re-declared or updated
const z = 10;
// z = 20; // Error: Assignment to constant variableAdditional Comment:
✅ Answered with JavaScript best practices.- "var" is function-scoped, meaning it is accessible within the function it is declared in, or globally if declared outside any function.
- "let" and "const" are block-scoped, meaning they are only accessible within the block they are declared in (e.g., inside a loop or an if statement).
- "const" variables must be initialized at the time of declaration and cannot be reassigned, but objects declared with "const" can have their properties modified.
- "let" allows you to declare variables that can be reassigned but not re-declared in the same scope.
Recommended Links:
