Variables and Data Types · Lesson 3 of 40
JavaScript Variables: let vs const vs var
A variable is a name for a value. JavaScript has three keywords to create one: const, let and the older var. Choosing correctly prevents a whole family of bugs – and "let vs const vs var" is one of the most common JavaScript interview questions.
In this lesson
constandlet, and when to use each- Why
varis avoided in modern code - Block scope, hoisting and the temporal dead zone
- Naming conventions
const and let
const city = "Bengaluru"; // cannot be reassigned
let visitors = 120; // can be reassigned
visitors = visitors + 30;
console.log(city, visitors);
const cart = ["rice"];
cart.push("dal"); // allowed: the array changes, the binding doesn't
console.log(cart);
Bengaluru 150 [ 'rice', 'dal' ]
The rule professionals follow
Use const by default. Switch to let only when the variable really must be reassigned (counters, accumulators). Never use var in new code. This makes reassignments stand out to readers.
const stops reassignment, not mutation: a const array or object can still change its contents. To freeze an object, use Object.freeze().
const max = 3;
try {
max = 5;
} catch (e) {
console.log(e.name + ": " + e.message);
}
TypeError: Assignment to constant variable.
Block scope
let and const exist only inside the block { } where they are declared:
const marks = [78, 32, 91];
let passed = 0;
for (const m of marks) {
const ok = m >= 40; // ok lives only inside the loop body
if (ok) passed++;
}
console.log(passed);
console.log(typeof ok); // not defined out here
2 undefined
Why var is avoided
var is function-scoped, not block-scoped, so it leaks out of blocks – the source of a classic bug:
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log("var:", i), 0);
}
for (let j = 0; j < 3; j++) {
setTimeout(() => console.log("let:", j), 0);
}
var: 3 var: 3 var: 3 let: 0 let: 1 let: 2
With var there is one shared i, which is 3 by the time the callbacks run. With let each loop iteration gets its own j.
var | let | const | |
|---|---|---|---|
| Scope | function | block | block |
| Reassign | yes | yes | no |
| Redeclare in same scope | yes | no | no |
| Use before declaration | undefined | ReferenceError (TDZ) | ReferenceError (TDZ) |
Hoisting and the temporal dead zone
Before running code, JavaScript registers all declarations in a scope – this is called hoisting. var variables are created and set to undefined; let and const are created but stay uninitialised until their line runs. The time in between is the temporal dead zone (TDZ).
console.log(a); // var: hoisted as undefined
var a = 10;
try {
console.log(b); // let: in the temporal dead zone
} catch (e) {
console.log(e.name + ": " + e.message);
}
let b = 20;
undefined ReferenceError: Cannot access 'b' before initialization
Life of a let variable
- Scope startsb is registered but uninitialised
- Temporal dead zoneany access → ReferenceError
- let b = 20 runsb is initialised
- Usableuntil the block ends
Naming
- Names may contain letters, digits,
_and$, can't start with a digit, and are case-sensitive. - Use camelCase for variables and functions (
totalPrice), PascalCase for classes (ShoppingCart), and UPPER_SNAKE_CASE for true constants (MAX_RETRIES). - Choose meaningful names:
daysUntilExam, notd.
Practice
Try it yourself
Swap two variables in one line using array destructuring.
Show solution
let left = "tea", right = "coffee"; [left, right] = [right, left]; console.log(left, right);coffee tea
Quick check
Can you push to a const array?
Yes – const prevents reassigning the variable, not changing the array.
What does console.log(x); var x = 5; print?
undefined – var declarations are hoisted and initialised to undefined.
What is the temporal dead zone?
The period between entering a scope and the let/const declaration line, during which accessing the variable throws a ReferenceError.
Summary
- Default to
const, useletfor reassignment, avoidvar. - let/const are block-scoped; var is function-scoped and leaks out of blocks.
- All declarations are hoisted; let/const sit in the temporal dead zone until declared.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.