-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Added new JavaScript questions to README #302
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | | ||
|
|
||
| <!-- TOC_END --> | ||
|
|
||
| <!-- QUESTIONS_START --> | ||
|
|
@@ -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'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| #### 1. What is the output of below code | ||
|
|
||