Facebook Pixel

What Is Scope in JavaScript?

Scope is where variables are accessible. Here is the difference between global, function, and block scope.

What Is Scope in JavaScript?

Scope is the set of rules that determines where variables are accessible. JavaScript has three main types of scope.

Global Scope

Variables declared outside any function or block are in the global scope. They are accessible everywhere.

const globalVar = "I am global"; function foo() { console.log(globalVar); // accessible } foo(); `` In browsers, `var` globals become properties of `window`. `let`/`const` globals live in a separate global lexical environment. ### Function Scope Variables declared inside a function are only accessible inside that function. ```js function foo() { const localVar = "I am local"; console.log(localVar); // accessible } foo(); console.log(localVar); // ReferenceError: localVar is not defined `` `var` is function-scoped: it is visible throughout the function, regardless of blocks inside it. ### Block Scope Variables declared with `let` and `const` inside a block (`{}`) are only accessible inside that block. ```js if (true) { const blockVar = "I am block-scoped"; console.log(blockVar); // accessible } console.log(blockVar); // ReferenceError `` `var` ignores block boundaries (except function bodies). `let` and `const` respect them. ### Module Scope In ES modules, top-level variables are scoped to the module, not global. They are only accessible if explicitly exported. ```js // module.js const secret = "private"; // not accessible outside this module export const publicVar = "public"; `` ### Why Scope Matters - **Encapsulation**: keep variables private to where they are needed. - **Avoiding collisions**: same name in different scopes does not conflict. - **Memory**: the engine can free variables when their scope ends. - **Predictability**: scoped variables are easier to reason about. ### The Takeaway Scope determines where variables are accessible. JS has global, function, block, and module scope. `var` is function-scoped; `let`/`const` are block-scoped. Module scope keeps top-level variables private to the module. Use the tightest scope that works.

Scope is the set of rules that determines where variables are accessible. JavaScript has global scope, function scope, block scope, and module scope.

Function scope means a variable is visible throughout the function (var). Block scope means a variable is visible only within the nearest enclosing block (let and const). var ignores block boundaries; let and const respect them.

Yes. let and const are block-scoped. They are only accessible within the nearest enclosing pair of braces. var is function-scoped and ignores block boundaries (except function bodies).

In ES modules, top-level variables are scoped to the module, not global. They are only accessible outside if explicitly exported. This prevents accidental global pollution.

It provides encapsulation (keep variables private), avoids naming collisions, allows the engine to free memory when scope ends, and makes code more predictable and easier to reason about.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.