Module scope keeps variables and functions private inside a file. This helps avoid mixing up names and keeps code organized.
Module scope in Javascript
Introduction
Syntax
Javascript
export const publicValue = 42; const privateValue = 10; function privateFunction() { return privateValue; } export function publicFunction() { return privateFunction() + publicValue; }
Variables and functions not exported stay private to the module.
Use export keyword to share parts of the module.
Examples
secret is private, visible is public and can be used in other files.Javascript
const secret = 'hidden'; export const visible = 'shown';
helper is private, only main is exported.Javascript
function helper() { return 5; } export function main() { return helper() * 2; }
Sample Program
In mathModule.js, secretNumber is private. Only addFive is exported and used in app.js.
Javascript
// file: mathModule.js const secretNumber = 7; export function addFive(num) { return num + secretNumber; } // file: app.js import { addFive } from './mathModule.js'; console.log(addFive(3));
Important Notes
Each JavaScript file is a module with its own scope by default when using ES modules.
Variables not exported cannot be accessed from other files.
Use import to bring exported parts into another module.
Summary
Module scope keeps code private inside files.
Use export to share code outside the module.
Helps avoid name conflicts and keeps code organized.
Practice
1. What does
module scope mean in JavaScript?easy
Solution
Step 1: Understand module scope concept
Module scope means code inside a file is private unless explicitly shared.Step 2: Compare options with this concept
Only Variables and functions are private to the file unless exported correctly states variables/functions are private unless exported.Final Answer:
Variables and functions are private to the file unless exported -> Option BQuick Check:
Module scope = private unless exported [OK]
Hint: Remember: module code is private unless exported [OK]
Common Mistakes:
- Thinking all variables are global
- Confusing module scope with block scope
- Believing functions must be inside loops
2. Which of the following is the correct way to export a function named
greet from a module?easy
Solution
Step 1: Recall correct export syntax
To export a function, useexport function functionName() {}.Step 2: Check each option
export function greet() {} matches the correct syntax; others are invalid or incorrect.Final Answer:
export function greet() {} -> Option AQuick Check:
Export function syntax = export function name() [OK]
Hint: Use 'export function' before function name [OK]
Common Mistakes:
- Placing export after function keyword
- Using incorrect export assignment syntax
- Missing parentheses in function declaration
3. Given this module code:
What is the output when this module runs?
const secret = 42; export const visible = 'hello'; console.log(typeof secret); console.log(typeof visible);
What is the output when this module runs?
medium
Solution
Step 1: Understand variable scope and export
Bothsecretandvisibleexist inside the module;secretis private,visibleis exported.Step 2: Check typeof outputs inside the same module
Inside the module, both variables exist, sotypeof secretis "number" andtypeof visibleis "string".Final Answer:
"number" and "string" -> Option AQuick Check:
typeof secret = number, typeof visible = string [OK]
Hint: Inside module, all declared variables exist [OK]
Common Mistakes:
- Thinking private variables are undefined inside module
- Confusing export with variable existence
- Expecting ReferenceError inside same file
4. What is wrong with this module code?
const count = 10; export count = 20;
medium
Solution
Step 1: Analyze export syntax
Export keyword cannot be used with assignment likeexport count = 20;. It must beexport const count = 20;or export existing variables.Step 2: Identify error type
This causes a syntax error because export expects declaration or named export, not assignment.Final Answer:
Syntax error: export cannot be used with assignment -> Option CQuick Check:
Export syntax must declare or export existing variables [OK]
Hint: Export must declare or export, not assign [OK]
Common Mistakes:
- Trying to export by assignment
- Missing const/let/var with export
- Assuming export can change variable value
5. You have two modules:
What will happen when
// file1.js
const secret = 'hidden';
export const data = 123;
// file2.js
import { data } from './file1.js';
console.log(secret);
console.log(data);What will happen when
file2.js runs?hard
Solution
Step 1: Understand module scope and imports
The variablesecretis private tofile1.jsand not exported, so it cannot be accessed infile2.js.Step 2: Predict runtime behavior
Trying to accesssecretinfile2.jscauses a ReferenceError. The importeddatalogs correctly.Final Answer:
ReferenceError: secret is not defined -> Option DQuick Check:
Private variables cause ReferenceError outside module [OK]
Hint: Only exported variables are accessible in other modules [OK]
Common Mistakes:
- Assuming private variables are imported automatically
- Confusing import syntax errors with runtime errors
- Expecting undefined instead of ReferenceError
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
