The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →JavaScript has three variable declaration keywords: var, let and const. In new code, use const when you will keep a name bound to the same value, and let when you need to reassign it. Learn var so you can understand older code and avoid its different scope and initialization behavior.
How var, let and const differ
The practical differences are scope, what happens if you access a binding before its declaration is reached, whether you can reassign it, and whether you can declare the same name again in the same scope.
As an Amazon Associate I earn from qualifying purchases.
| Keyword | Scope | Access before initialization | Reassignment | Redeclaration in the same scope |
|---|---|---|---|---|
var |
Function scope, or global scope outside a function | Returns undefined before the initializer runs |
Allowed | Allowed |
let |
Block scope | Throws ReferenceError in the temporal dead zone |
Allowed | Not allowed; it is a syntax error |
const |
Block scope | Throws ReferenceError in the temporal dead zone |
Not allowed | Not allowed; it is a syntax error |
What scope means for each keyword
var ignores block boundaries
A var declared inside a function belongs to that function, not just to the nearest pair of braces. If declared outside a function, it has global scope. For example, after this block runs, count is still accessible:
if (true) {
var count = 1;
}
console.log(count); // 1
let and const stay inside the block
A block is code enclosed in braces, such as an if body or loop body. A let or const declared there is available only within that block:
#1 Best Overall
if (true) {
let message = "inside";
const limit = 3;
}
// message and limit are not available here
What happens before a declaration is reached
var is declared before execution, but its assignment is not
A var binding is processed before code in its scope executes. Its initializer still runs at the line where it appears, so reading the name earlier produces undefined, not the later assigned value:
console.log(score); // undefined
var score = 10;
let and const have a temporal dead zone
A lexical binding declared with let or const exists in its scope before the declaration line, but code cannot access it until execution reaches and initializes that declaration. The intervening period is called the temporal dead zone (TDZ). Access during it throws a ReferenceError:
Rank #2
console.log(score); // ReferenceError
let score = 10;
Even typeof throws if the name is a lexical binding currently in the TDZ. MDN defines the TDZ as the interval “from the start of the block until code execution reaches the place where the variable is declared and initialized.” See MDN’s let reference. Whether to describe let and const as “hoisted” is debated; the TDZ is the behavior to understand.
Free tools Windows power users keep installed
One-click scans. No signup required.
Reassignment, redeclaration and const objects
Use let when a binding needs a new value
let allows reassignment after declaration, but you cannot declare that same name again in the same scope:
let status = "pending";
status = "complete"; // allowed
// let status = "failed"; // SyntaxError in the same scope
const prevents rebinding, not mutation
A const declaration must include an initializer, and the identifier cannot later be assigned a different value. If it refers to an object or array, however, the object’s properties or contents can still change:
const settings = { theme: "light" };
settings.theme = "dark"; // allowed
// settings = {}; // TypeError: cannot reassign
The useful distinction is whether the binding changes, not whether the referenced data can change. Prefer const when the binding remains assigned to the same value; choose let when it must point to a different value.
Rank #4
Redeclaring names and global declarations
var permits duplicate declarations of the same name in the same scope. let and const do not: attempting to redeclare either in that scope is a syntax error. Reassigning an existing let is different from redeclaring it.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchIn a classic script, a top-level var declaration creates a property on the global object. Top-level let and const do not. JavaScript modules instead use module scope, so “global” behavior depends on whether the code is a classic script or a module.
Quick Recap
Best Value
A practical choice for new code
- Start with
constwhen you do not need to assign a different value to the name. - Use
letwhen the name needs reassignment, such as a counter that changes in a loop. - Use
varwhen working with code that already relies on its function-scoped or redeclaration behavior; do not assume it behaves likelet.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




