Skip to content

Commit 63961b6

Browse files
committed
Improve answer for 'What is Module Scope in JavaScript?'
1 parent 6064d6e commit 63961b6

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

README.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -8961,34 +8961,34 @@ The `globalThis` property provides a standard way of accessing the global object
89618961
3. Variables and functions need to be explicitly exported to be used in other modules
89628962
4. The global scope cannot access module variables unless they are explicitly exported and imported
89638963
5. Modules are always executed in strict mode
8964-
8965-
```javascript
8966-
// moduleA.js
8964+
8965+
```javascript
8966+
// moduleA.js
89678967

8968-
// This variable is PRIVATE to moduleA. It's like a tool inside a closed box.
8969-
const privateVariable = "I am private";
8968+
// This variable is PRIVATE to moduleA. It's like a tool inside a closed box.
8969+
const privateVariable = "I am private";
89708970

8971-
// This variable is PUBLIC because it's exported. Others can use it when they import moduleA.
8972-
export const publicVariable = "I am public";
8971+
// This variable is PUBLIC because it's exported. Others can use it when they import moduleA.
8972+
export const publicVariable = "I am public";
89738973

8974-
// PUBLIC function because it's exported. But it can still access privateVariable inside moduleA.
8975-
export function publicFunction() {
8976-
console.log(privateVariable); // ✅ This works because we're inside the same module.
8977-
return "Hello from publicFunction!";
8978-
}
8974+
// PUBLIC function because it's exported. But it can still access privateVariable inside moduleA.
8975+
export function publicFunction() {
8976+
console.log(privateVariable); // ✅ This works because we're inside the same module.
8977+
return "Hello from publicFunction!";
8978+
}
89798979

8980-
// moduleB.js
8980+
// moduleB.js
89818981

8982-
// Importing PUBLIC items from moduleA.
8983-
import { publicVariable, publicFunction } from './moduleA.js';
8982+
// Importing PUBLIC items from moduleA.
8983+
import { publicVariable, publicFunction } from './moduleA.js';
89848984

8985-
console.log(publicVariable); // ✅ "I am public" - Works because it's exported.
8986-
console.log(publicFunction()); // ✅ "Hello from publicFunction!" - Works as well.
8985+
console.log(publicVariable); // ✅ "I am public" - Works because it's exported.
8986+
console.log(publicFunction()); // ✅ "Hello from publicFunction!" - Works as well.
89878987

8988-
// ❌ This will cause an ERROR because privateVariable was NOT exported from moduleA.
8989-
// console.log(privateVariable); // ❌ ReferenceError: privateVariable is not defined
8988+
// ❌ This will cause an ERROR because privateVariable was NOT exported from moduleA.
8989+
// console.log(privateVariable); // ❌ ReferenceError: privateVariable is not defined
89908990

8991-
```
8991+
```
89928992
Common use cases and benefits:
89938993
89948994
- Encapsulation of module-specific code

0 commit comments

Comments
 (0)