Skip to content
Concept 05 // Module 01: JavaScript Foundations & Environment

Variables: var, let & const

Module 01: JavaScript Foundations & Environment•Concept 05 of 100• 5 min read

Variables: var, let & const

var, let, and const all declare variables, but they differ in scope (where the name is visible), hoisting/TDZ behavior, and reassignment rules. Choosing the right one avoids leaks, surprises, and accidental globals.

The Core Concept

Syntax Reference
// Declarations
var name1 = value;    // Function-scoped
let name2 = value;    // Block-scoped
const name3 = value;  // Block-scoped & immutable binding

// Scope Blocks
{
  // "Block" scope created here
  let visibleHere = 1;
}

function name() {
  // "Function" scope created here
  var visibleHere = 1;
}

// Reassignment Syntax
name1 = newValue;     // Allowed
name2 = newValue;     // Allowed
name3 = newValue;     // Error!

// Hoisting Behavior
// var: hoisted as undefined
// let/const: hoisted but uninitialized (TDZ)

Live Interactive Studio

`var` ignores block boundaries (like if/for loops), leaking into the outer function. `let`/`const` stay safely inside.
JavaScript Editor
Loading...
Console Output
Waiting for output...

Key Code Patterns in Action

01

Block Scope vs Function Scope

// 1. Block Scope vs Function Scope
if (true) {
  var functionScoped = 'I leak out!';
  let blockScoped = 'I stay here';
  const alsoBlockScoped = 'Me too';
}

console.log(functionScoped); // "I leak out!"
// console.log(blockScoped); // ReferenceError
// console.log(alsoBlockScoped); // ReferenceError
`var` ignores block boundaries (like if/for loops), leaking into the outer function. `let`/`const` stay safely inside.
02

The "Loop Problem" (Closure + Scope)

// 2. The "Loop Problem" (Closure + Scope)
// OLD WAY (Buggy): var shares one 'i' variable
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log('var:', i), 100);
}
// Output: var: 3, var: 3, var: 3

// MODERN WAY (Fixed): let creates a new 'j' for each loop
for (let j = 0; j < 3; j++) {
  setTimeout(() => console.log('let:', j), 100);
}
// Output: let: 0, let: 1, let: 2
Using `let` in loops ensures each iteration gets its own independent variable binding, fixing async bugs.
03

Const Mutability

// 3. Const Mutability
const user = { name: "Subhajit" };

// Allowed: Modifying the object's contents
user.name = "Alex"; 
console.log(user.name); // "Alex"

// Forbidden: Replacing the object itself
// user = { name: "New Person" }; // TypeError: Assignment to constant variable
`const` protects the *variable binding* (the arrow pointing to memory), not the *value* (the object itself).
04

Temporal Dead Zone (TDZ) Safety

// 4. Temporal Dead Zone (TDZ) Safety
console.log(myVar); // undefined (risky!)
var myVar = 10;

// console.log(myLet); // ReferenceError (safe!)
let myLet = 20;
TDZ ensures you don't use variables before they are ready. `var` lets you access them too early (as undefined), which hides bugs.
05

Scope Differences: Function vs Block

{ var v = 1; let l = 2; const c = 3; }
console.log('var leaks out:', v);
// l and c are safely confined to the block!
var is function-scoped and leaks out of blocks; let and const are strictly block-scoped.
06

Temporal Dead Zone (TDZ)

// Accessing let/const before initialization throws ReferenceError:
try {
  console.log(score); 
} catch (e) {
  console.log('TDZ Error:', e.name); // ReferenceError
}
let score = 100;
let and const exist in the Temporal Dead Zone from block entry until their declaration.
07

const Immutability vs Mutation

const user = { name: 'Subhajit' };
// user = {}; // ⚠️ TypeError: Assignment to constant variable
user.name = 'Alex'; // Mutating internal property is allowed!
console.log(user.name);
const prevents binding reassignment, but does not deeply freeze object contents.

Real-World Engineering Takeaways

Use `const` by default to signal intent and prevent accidental rebinding.
Use `let` only when the value must change (counters, accumulators, loop indices).
Avoid `var` in modern code to prevent block leaks and hoisting surprises.
Async loops: prefer `let` so each iteration gets its own binding (fixes the classic 'all threes' bug).

Under the Hood: Engine & Memory Mechanics

How JavaScript Executes This
**Storage Locations:** - **Primitives (number, boolean, undefined):** Stored directly in the **Stack** (fast access). - **Objects/Functions:** Stored in the **Heap** (large memory); the Stack holds a *reference* (pointer) to the Heap address. **Browser Execution Context:** - **Variables:** Stored in the Variable Environment of the current Execution Context (Stack). - **Closures:** Stored in the Heap (so they survive after the function returns). **Hardware:** - **CPU:** Executes the instructions (Stack operations are CPU-cache friendly). - **RAM:** Physical home for both Stack and Heap. **Creation Phase:** 1. Engine parses code. 2. Allocates memory for variables (hoisting). - `var` → initialized to `undefined`. - `let/const` → uninitialized (TDZ). **Execution Phase:** 1. Code runs line-by-line. 2. Assignments happen. 3. Accessing TDZ variables throws errors.

Quick Knowledge Check

Test your understanding on these 10 questions (one by one):

Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model

What is the core purpose of Variables: var, let & const in JavaScript?

// Concept 5: Variables: var, let & const
// Understanding the core mental model
1 / 10