Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new JavaScript questions to README #302

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@
| 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) |
<!-- TOC_END -->

<!-- QUESTIONS_START -->
Expand Down Expand Up @@ -8950,6 +8951,44 @@ 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';
Copy link
Owner

@sudheerj sudheerj Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jenesh18 It seems you are trying to access 'privateVariable' instead 'publicFunction' from moduleA. BTW, you are not using publicFunction anywhere in moduleB

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sudheerj, I’ve updated the answer based on your feedback. The incorrect reference to privateVariable has been removed, and the usage of publicFunction has been added in the example. and I created a new PR Please review it when you get a chance. Thanks for your time!


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)**

<!-- QUESTIONS_END -->

### Coding Exercise
Expand Down