What is the difference between "let" and "var"?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In modern JavaScript, both let and var declare variables, but they follow different scoping and hoisting rules. The difference matters because var often allows bugs that let prevents, especially inside loops, conditionals, and larger functions.
Scope: function scope versus block scope
var is function-scoped. let is block-scoped. That is the biggest practical difference.
With var, the variable exists throughout the entire function, even though it was written inside the if block. With let, the variable only exists inside that block.
That makes let a safer default because the lifetime of the variable matches the visible structure of the code.
Hoisting and the temporal dead zone
Both var and let are hoisted, but they behave differently before initialization.
With var, the declaration is hoisted and initialized to undefined.
With let, the variable exists but cannot be used before the declaration line:
This period before initialization is called the temporal dead zone. It is a good thing because it catches mistakes earlier instead of silently giving you undefined.
Re-declaration rules
var allows re-declaration in the same scope:
let does not:
This rule makes accidental duplicate declarations easier to spot during development.
Loop behavior is often where bugs appear
Classic loop bugs are one of the strongest arguments for let.
That prints 3 three times, because all callbacks close over the same function-scoped i.
Now compare it with let:
That prints 0, 1, and 2, because each loop iteration gets its own block-scoped binding.
Global behavior
A global var declaration becomes a property of the browser window object. A global let declaration does not.
This is another reason let behaves more predictably in modern applications.
When to use each one
For current JavaScript code, let is generally preferred over var. In many cases, const is even better when the binding should not be reassigned.
Use let when:
- the variable will be reassigned,
- the variable should exist only in a specific block,
- or you want safer, modern scoping behavior.
Use var mainly when maintaining older code or when you deliberately need its historical function-scoped behavior, which is rare in new code.
Common Pitfalls
The biggest mistake is assuming var respects block boundaries. It does not. Variables declared with var inside if, for, or while still leak to the surrounding function scope.
Another common issue is using a let variable before the declaration line and expecting undefined. Instead, JavaScript throws a ReferenceError because of the temporal dead zone.
Be careful not to confuse "can be reassigned" with "can be redeclared." let allows reassignment but not re-declaration in the same scope.
Finally, if you are writing new code, do not stop at choosing between var and let. Also ask whether the variable should actually be const, because that communicates intent even more clearly.
Summary
- '
varis function-scoped, whileletis block-scoped.' - '
varis hoisted and initialized toundefined;letis hoisted but inaccessible before initialization.' - '
varallows re-declaration in the same scope;letdoes not.' - '
letbehaves correctly in loop closures and avoids many old JavaScript scoping bugs.' - In modern code, prefer
letovervar, and preferconstwhen reassignment is not needed.

