|
518 | 518 | | 468 | [How to find the number of parameters expected by a function?](#how-to-find-the-number-of-parameters-expected-by-a-function) |
|
519 | 519 | | 469 | [What is globalThis, and what is the importance of it?](#what-is-globalthis-and-what-is-the-importance-of-it) |
|
520 | 520 | | 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) | |
521 | 522 | <!-- TOC_END -->
|
522 | 523 |
|
523 | 524 | <!-- QUESTIONS_START -->
|
@@ -8967,6 +8968,54 @@ The `globalThis` property provides a standard way of accessing the global object
|
8967 | 8968 |
|
8968 | 8969 | **[⬆ Back to Top](#table-of-contents)**
|
8969 | 8970 |
|
| 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 | +
|
8970 | 9019 | <!-- QUESTIONS_END -->
|
8971 | 9020 |
|
8972 | 9021 | ### Coding Exercise
|
|
0 commit comments