Day 3 — Scope & Hoisting

Story-based visuals • Animations • Live playground • Mini-quiz

🎬 The JS House — Understanding Scope

Global (Main Hall)
var username = "Alex"
Function Scope (Kitchen)
var item = "Batter"
Block Scope (Bedroom)
let secret = "Chocolate"

Rule: children can see parents, parents cannot see children. GlobalFunctionBlock (let/const)

⚡ Two Phases — Hoisting Visual

Declarations rise
Assignments run
Compilation (Memory): var a;function greet(){...}
Execution (Run time): a = 2;greet();

Declarations are “lifted” to the top of their scope before execution begins. Function declarations hoist with bodies; variables hoist as names (initially undefined).

🧪 Practice Playground (Sandboxed)

Tip: use console.log()

Console (isolated iframe)

📝 Pocket Rules

🧠 Quick Quiz

What is printed?
console.log(a);
var a = 10;
Which is true?
Output?
foo(); // ?
var foo;
function foo(){ console.log(1); }
foo = function(){ console.log(2); };