JS Fundamentals
Variables — var, let, const
Master JavaScript variable declarations. Compare var, let, and const in terms of scope, hoisting, redeclaration, and temporal dead zones.
1. Introduction
Variables are the basic building blocks of any program, storing values in memory for future access. In JavaScript, how you declare a variable determines its visibility (scope), lifetime, execution lifecycle, and whether its value can change.
2. Why It Matters
Understanding variables prevents bugs caused by scope leaks, variable shadowing, unexpected runtime reference errors, and mutation issues on read-only constant bindings.
3. Real-World Analogy
Think of variable scopes like Containers in a Workspace:
- var (Global Whiteboard): Accessible from anywhere in the room. Anyone can change it, wipe it, or overwrite it, which occasionally leads to confusion about who wrote what.
- let (Individual Notepad): Belongs to a specific desk or task. Once you leave the desk (block), you can't read it anymore. You can change your notes, but no one outside your group can see it.
- const (Laminated Reference Sheet): A block-scoped sheet containing reference values. The sheet itself cannot be replaced by a different page, though you can still write updates on any dynamic tables printed on it.
4. How It Works
Variables in JavaScript behave differently based on their declaration keyword:
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function Scope | Block Scope | Block Scope |
| Hoisting | Yes (initialised to undefined) |
Yes (uninitialised, in TDZ) | Yes (uninitialised, in TDZ) |
| Reassignable | Yes | Yes | No |
| Redeclarable | Yes | No | No |
5. Internal Architecture
During the memory creation phase of an execution context:
• var: The engine assigns the name and initialises it to undefined immediately.
• let/const: The engine registers the name in memory but does not initialise it. The variable remains in a Temporal Dead Zone (TDZ) until execution reaches the line where it is declared.
6. Visual Explanation
7. Practical Example
Here is an example showing block scoping versus function scoping:
8. Common Mistakes
- Mutating objects declared with const:
constonly guarantees that the binding to the memory address cannot change. It does not freeze the object content. - Accessing let/const before declaration: Triggering a ReferenceError due to the Temporal Dead Zone.
9. Quick Quiz
Q1: What happens if you attempt to access a let variable before it is declared?
A) It returns undefined
B) It throws a ReferenceError
Answer: B — The let variable sits in the Temporal Dead Zone until its declaration line is evaluated, resulting in a ReferenceError.
10. Scenario-Based Challenge
The Loop Variable Closure Challenge:
Analyze why a loop declaring var i prints the final loop counter index inside setTimeouts, while let i captures index values correctly at each step. Trace the scope differences.
11. Debugging Exercise
Explain why this code crashes, and how to fix it:
console.log(value);
let value = 42;
View Solution
Diagnosis: The engine raises a ReferenceError: Cannot access 'value' before initialization because let value is hoisted but resides in the Temporal Dead Zone at line 1.
Fix: Declare the variable before accessing it:
let value = 42;
console.log(value);
12. Interview Questions
🟢 Q1: Explain the Temporal Dead Zone (TDZ) in JavaScript.
Answer: The Temporal Dead Zone is the period of execution beginning at the entry of a block scope until the line where a variable declared with let or const is initialized. During this time, the variable cannot be referenced or accessed in any way.
13. Production Considerations
- • Prefer const: Default to using
constfor all declarations. Only useletif the variable must be reassigned. Avoid usingvarentirely in modern codebases.