Skip to content

Commit e43ea98

Browse files
authored
Merge pull request #304 from Jenesh18/feature-module-scope-update
Improved the explanation for 'What is Module Scope in JavaScript?' with better code examples and clear explanations.
2 parents 34549e3 + 63961b6 commit e43ea98

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@
518518
| 468 | [How to find the number of parameters expected by a function?](#how-to-find-the-number-of-parameters-expected-by-a-function) |
519519
| 469 | [What is globalThis, and what is the importance of it?](#what-is-globalthis-and-what-is-the-importance-of-it) |
520520
| 470 | [What are the array mutation methods?](#what-are-the-array-mutation-methods) |
521+
| 471 | [What is module scope in JavaScript?](#what-is-module-scope-in-JavaScript) |
521522
<!-- TOC_END -->
522523

523524
<!-- QUESTIONS_START -->
@@ -8967,6 +8968,54 @@ The `globalThis` property provides a standard way of accessing the global object
89678968
89688969
**[⬆ Back to Top](#table-of-contents)**
89698970
8971+
471. ### What is module scope in JavaScript?
8972+
Module scope is a feature introduced with ES6 (ES2015) modules that creates a scope specific to a module file, isolating variables and functions declared within it from the global scope and other modules. Variables and functions declared in a module are private by default and can only be accessed by other modules if they are explicitly exported.
8973+
8974+
Key characteristics of module scope:
8975+
8976+
1. Variables declared in a module are scoped to that module only.
8977+
2. Each module has its own top-level scope
8978+
3. Variables and functions need to be explicitly exported to be used in other modules
8979+
4. The global scope cannot access module variables unless they are explicitly exported and imported
8980+
5. Modules are always executed in strict mode
8981+
8982+
```javascript
8983+
// moduleA.js
8984+
8985+
// This variable is PRIVATE to moduleA. It's like a tool inside a closed box.
8986+
const privateVariable = "I am private";
8987+
8988+
// This variable is PUBLIC because it's exported. Others can use it when they import moduleA.
8989+
export const publicVariable = "I am public";
8990+
8991+
// PUBLIC function because it's exported. But it can still access privateVariable inside moduleA.
8992+
export function publicFunction() {
8993+
console.log(privateVariable); // ✅ This works because we're inside the same module.
8994+
return "Hello from publicFunction!";
8995+
}
8996+
8997+
// moduleB.js
8998+
8999+
// Importing PUBLIC items from moduleA.
9000+
import { publicVariable, publicFunction } from './moduleA.js';
9001+
9002+
console.log(publicVariable); // ✅ "I am public" - Works because it's exported.
9003+
console.log(publicFunction()); // ✅ "Hello from publicFunction!" - Works as well.
9004+
9005+
// ❌ This will cause an ERROR because privateVariable was NOT exported from moduleA.
9006+
// console.log(privateVariable); // ❌ ReferenceError: privateVariable is not defined
9007+
9008+
```
9009+
Common use cases and benefits:
9010+
9011+
- Encapsulation of module-specific code
9012+
- Prevention of global scope pollution
9013+
- Better code organization and maintenance
9014+
- Explicit dependency management
9015+
- Protection of private implementation details
9016+
9017+
**[⬆ Back to Top](#table-of-contents)**
9018+
89709019
<!-- QUESTIONS_END -->
89719020
89729021
### Coding Exercise

0 commit comments

Comments
 (0)