TGViewer
Web Development - HTML, CSS & JavaScript Web Development - HTML, CSS & JavaScript @javascript_courses · 55.7K subscribers
Post #3737 160
When "inner()" looks for:

name

it finds it in its own scope.

For:

city

JavaScript looks in the outer scope.

For:

country

it goes one level further outward.

Conceptually:

inner scope
↓
outer scope
↓
global/module scope


This is the scope chain.

8️⃣ What Is Hoisting?

Hoisting describes how JavaScript's execution process makes certain declarations available before their position in the source code is reached.

But different declarations behave differently.

This is why you should avoid thinking of hoisting as simply:

"JavaScript moves all code to the top."

That's not an accurate mental model.

9️⃣ Function Declaration Hoisting

Function declarations can be called before their declaration in the same scope.

greet();

function greet() {
console.log("Hello");
}


Output:

Hello


The function declaration is available when execution begins.

🔟 "var" Hoisting

Consider:

console.log(age);

var age = 25;


The result is:

undefined


Conceptually, you can think of the declaration being initialized early:

var age;

console.log(age);

age = 25;


The important point is:

var declaration → initialized as undefined

before normal execution reaches the assignment.

1️⃣1️⃣ "let" and "const" Hoisting

Now consider:

console.log(age);

let age = 25;


This produces a ReferenceError.

The same applies to:

console.log(age);

const age = 25;


Why?

"let" and "const" declarations are created during the setup phase, but they aren't initialized until execution reaches their declaration.

The period between entering the scope and initialization is called the:

Temporal Dead Zone (TDZ)

1️⃣2️⃣ Temporal Dead Zone

Example:

{
console.log(name);

let name = "Rahul";
}


The variable "name" exists in the scope, but accessing it before initialization causes an error.

Conceptually:

Block begins
↓
name exists but is uninitialized
↓
Temporal Dead Zone
↓
let name = "Rahul"
↓
name becomes initialized


So:

console.log(name);
let name = "Rahul";


is not equivalent to using "var".

1️⃣3️⃣ Why "let" and "const" Are Safer

Compare:

console.log(value);

var value = 10;


Output:

undefined


versus:

console.log(value);

let value = 10;


Error:

ReferenceError


The error makes the problem much easier to detect instead of silently giving you undefined.

1️⃣4️⃣ Function Scope vs Block Scope Example

Consider:

function test() {

if (true) {
let a = 10;
var b = 20;
}

console.log(b);
}

test();


This works because "b" is function-scoped.

But:

function test() {

if (true) {
let a = 10;
}

console.log(a);
}
More from @javascript_courses
  1. Oct 12, 2026doesn't work because "a" is block-scoped. ⭐ 1️⃣5️⃣ What Is an Execution Context? When Java…
  2. Oct 12, 2026💻 JavaScript Roadmap 2026 — Part 9 🟡 Scope, Hoisting & Execution Context Now we're movin…
  3. Oct 12, 2026🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲? Want to buil…
  4. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  5. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  6. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →