TGViewer
Web Development - HTML, CSS & JavaScript Web Development - HTML, CSS & JavaScript @javascript_courses ยท 55.7K subscribers
Post #3736 168
๐Ÿ’ป JavaScript Roadmap 2026 โ€” Part 9

๐ŸŸก Scope, Hoisting & Execution Context

Now we're moving from basic JavaScript syntax into concepts that explain how JavaScript actually executes your code.

These concepts are extremely important because they explain things like:

โ€ข Why some variables are accessible in certain places but not others

โ€ข Why functions can sometimes be called before they are written

โ€ข Why "let" and "const" behave differently from "var"

โ€ข How JavaScript manages function calls

โ€ข How nested functions access variables from outer functions

1๏ธโƒฃ What Is Scope?

Scope determines where a variable can be accessed in your program.

For example:

let name = "Rahul";

console.log(name);


The variable "name" is accessible here because it was declared in the outer scope.

But:

function greet() {
let message = "Hello";

console.log(message);
}

greet();


"message" is available inside "greet()".

Trying to access it outside:

console.log(message);


causes an error because "message" belongs to the function's local scope.

2๏ธโƒฃ Global Scope

A variable declared outside functions and blocks is generally in the global/module scope depending on how your JavaScript is executed.

Example:

const appName = "My App";

function showAppName() {
console.log(appName);
}

showAppName();


Output:

My App


The function can access "appName" because the variable exists in an outer scope.

3๏ธโƒฃ Function Scope

Variables declared inside a function are accessible within that function.

function calculate() {
const price = 1000;

console.log(price);
}

calculate();


This works.

But:

console.log(price);


doesn't work because "price" exists only inside "calculate()".

4๏ธโƒฃ Block Scope

A block is code surrounded by "{}".

For example:

if (true) {
let message = "Hello";

console.log(message);
}


"message" exists inside that block.

But:

console.log(message);


causes an error.

Variables declared using:

โ€ข let

โ€ข const

are block-scoped.

5๏ธโƒฃ "var" and Block Scope

This is one reason "var" can cause confusing behavior.

if (true) {
var message = "Hello";
}

console.log(message);


This can print:

Hello


because "var" is not block-scoped.

Compare:

if (true) {
let message = "Hello";
}

console.log(message);


This causes an error because "let" is block-scoped.

Remember:

โ€ข var โ†’ function-scoped

โ€ข let โ†’ block-scoped

โ€ข const โ†’ block-scoped

6๏ธโƒฃ Lexical Scope

JavaScript uses lexical scoping.

This means the scope of a variable is determined by where the code is written, not where the function is eventually called.

Example:

const name = "Rahul";

function outer() {

function inner() {
console.log(name);
}

inner();
}

outer();


"inner()" can access "name" because "name" exists in an outer lexical scope.

7๏ธโƒฃ Scope Chain

What happens if JavaScript can't find a variable in the current scope?

It looks outward.

Example:

const country = "India";

function outer() {

const city = "Pune";

function inner() {

const name = "Rahul";

console.log(name);
console.log(city);
console.log(country);
}

inner();
}

outer();
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, 2026When "inner()" looks for: name it finds it in its own scope. For: city JavaScript looks inโ€ฆ
  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 โ†’