From fcb0fdb9b5c6ff5480a36d5ac1dbcafdec142154 Mon Sep 17 00:00:00 2001 From: Jenesh Date: Sat, 18 Jan 2025 13:35:38 +0530 Subject: [PATCH 1/4] Added new JavaScript questions and solutions to README.md --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index cd7fe811..a7354db9 100644 --- a/README.md +++ b/README.md @@ -518,6 +518,8 @@ | 468 | [How to find the number of parameters expected by a function?](#how-to-find-the-number-of-parameters-expected-by-a-function) | | 469 | [What is globalThis, and what is the importance of it?](#what-is-globalthis-and-what-is-the-importance-of-it) | | 470 | [What are the array mutation methods?](#what-are-the-array-mutation-methods) | +| 471 | [What is module scope in JavaScript?](#what-is-module-scope-in-JavaScript) | + @@ -8950,8 +8952,47 @@ The `globalThis` property provides a standard way of accessing the global object **[⬆ Back to Top](#table-of-contents)** +471. ### What is module scope in JavaScript? + 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. + + Key characteristics of module scope: + + 1. Variables declared in a module are scoped to that module only. + 2. Each module has its own top-level scope + 3. Variables and functions need to be explicitly exported to be used in other modules + 4. The global scope cannot access module variables unless they are explicitly exported and imported + 5. Modules are always executed in strict mode + + ```javascript + // moduleA.js + const privateVariable = "I am private"; + export const publicVariable = "I am public"; + + export function publicFunction() { + console.log(privateVariable); // Works - can access private variables + return "Public function"; + } + + // moduleB.js + import { publicVariable, publicFunction } from './moduleA.js'; + + console.log(publicVariable); // "I am public" + console.log(privateVariable); // ReferenceError: privateVariable is not defined + + ``` + Common use cases and benefits: + + - Encapsulation of module-specific code + - Prevention of global scope pollution + - Better code organization and maintenance + - Explicit dependency management + - Protection of private implementation details + +**[⬆ Back to Top](#table-of-contents)** + + ### Coding Exercise #### 1. What is the output of below code From 7e9dbeb84b085238f5708ccd184034b553f2bdbe Mon Sep 17 00:00:00 2001 From: Jenesh Date: Sat, 18 Jan 2025 14:51:46 +0530 Subject: [PATCH 2/4] Added new JavaScript questions and solutions to README --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index a7354db9..6901da9c 100644 --- a/README.md +++ b/README.md @@ -519,7 +519,6 @@ | 469 | [What is globalThis, and what is the importance of it?](#what-is-globalthis-and-what-is-the-importance-of-it) | | 470 | [What are the array mutation methods?](#what-are-the-array-mutation-methods) | | 471 | [What is module scope in JavaScript?](#what-is-module-scope-in-JavaScript) | - @@ -8992,7 +8991,6 @@ The `globalThis` property provides a standard way of accessing the global object - ### Coding Exercise #### 1. What is the output of below code From 6064d6e4915a7081eb6711f7ebdbbab0c49eb0ba Mon Sep 17 00:00:00 2001 From: Jenesh Date: Mon, 3 Feb 2025 01:18:24 +0530 Subject: [PATCH 3/4] Improve answer for 'What is Module Scope in JavaScript?' --- README.md | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 6901da9c..4ccbc7ed 100644 --- a/README.md +++ b/README.md @@ -8961,24 +8961,34 @@ The `globalThis` property provides a standard way of accessing the global object 3. Variables and functions need to be explicitly exported to be used in other modules 4. The global scope cannot access module variables unless they are explicitly exported and imported 5. Modules are always executed in strict mode - - ```javascript - // moduleA.js - const privateVariable = "I am private"; - export const publicVariable = "I am public"; + + ```javascript + // moduleA.js - export function publicFunction() { - console.log(privateVariable); // Works - can access private variables - return "Public function"; - } + // This variable is PRIVATE to moduleA. It's like a tool inside a closed box. + const privateVariable = "I am private"; - // moduleB.js - import { publicVariable, publicFunction } from './moduleA.js'; + // This variable is PUBLIC because it's exported. Others can use it when they import moduleA. + export const publicVariable = "I am public"; - console.log(publicVariable); // "I am public" - console.log(privateVariable); // ReferenceError: privateVariable is not defined - - ``` + // PUBLIC function because it's exported. But it can still access privateVariable inside moduleA. + export function publicFunction() { + console.log(privateVariable); // ✅ This works because we're inside the same module. + return "Hello from publicFunction!"; + } + + // moduleB.js + + // Importing PUBLIC items from moduleA. + import { publicVariable, publicFunction } from './moduleA.js'; + + console.log(publicVariable); // ✅ "I am public" - Works because it's exported. + console.log(publicFunction()); // ✅ "Hello from publicFunction!" - Works as well. + + // ❌ This will cause an ERROR because privateVariable was NOT exported from moduleA. + // console.log(privateVariable); // ❌ ReferenceError: privateVariable is not defined + + ``` Common use cases and benefits: - Encapsulation of module-specific code From 63961b678da6addc82eb62a11e883c2d9a3a6b2e Mon Sep 17 00:00:00 2001 From: Jenesh Date: Mon, 3 Feb 2025 01:25:16 +0530 Subject: [PATCH 4/4] Improve answer for 'What is Module Scope in JavaScript?' --- README.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 4ccbc7ed..427ec675 100644 --- a/README.md +++ b/README.md @@ -8961,34 +8961,34 @@ The `globalThis` property provides a standard way of accessing the global object 3. Variables and functions need to be explicitly exported to be used in other modules 4. The global scope cannot access module variables unless they are explicitly exported and imported 5. Modules are always executed in strict mode - - ```javascript - // moduleA.js + + ```javascript + // moduleA.js - // This variable is PRIVATE to moduleA. It's like a tool inside a closed box. - const privateVariable = "I am private"; + // This variable is PRIVATE to moduleA. It's like a tool inside a closed box. + const privateVariable = "I am private"; - // This variable is PUBLIC because it's exported. Others can use it when they import moduleA. - export const publicVariable = "I am public"; + // This variable is PUBLIC because it's exported. Others can use it when they import moduleA. + export const publicVariable = "I am public"; - // PUBLIC function because it's exported. But it can still access privateVariable inside moduleA. - export function publicFunction() { - console.log(privateVariable); // ✅ This works because we're inside the same module. - return "Hello from publicFunction!"; - } + // PUBLIC function because it's exported. But it can still access privateVariable inside moduleA. + export function publicFunction() { + console.log(privateVariable); // ✅ This works because we're inside the same module. + return "Hello from publicFunction!"; + } - // moduleB.js + // moduleB.js - // Importing PUBLIC items from moduleA. - import { publicVariable, publicFunction } from './moduleA.js'; + // Importing PUBLIC items from moduleA. + import { publicVariable, publicFunction } from './moduleA.js'; - console.log(publicVariable); // ✅ "I am public" - Works because it's exported. - console.log(publicFunction()); // ✅ "Hello from publicFunction!" - Works as well. + console.log(publicVariable); // ✅ "I am public" - Works because it's exported. + console.log(publicFunction()); // ✅ "Hello from publicFunction!" - Works as well. - // ❌ This will cause an ERROR because privateVariable was NOT exported from moduleA. - // console.log(privateVariable); // ❌ ReferenceError: privateVariable is not defined + // ❌ This will cause an ERROR because privateVariable was NOT exported from moduleA. + // console.log(privateVariable); // ❌ ReferenceError: privateVariable is not defined - ``` + ``` Common use cases and benefits: - Encapsulation of module-specific code