Module scope in Javascript - Time & Space Complexity
When we use modules in JavaScript, we want to know how the program's speed changes as the code grows.
We ask: How does the time to run code inside a module change with more code or imports?
Analyze the time complexity of the following code snippet.
// module.js
export function greet(name) {
return `Hello, ${name}!`;
}
// main.js
import { greet } from './module.js';
console.log(greet('Alice'));
This code defines a simple function in a module and uses it in another file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the greet function once.
- How many times: Exactly one time in this example.
Since the function runs once, the time remains constant as code size grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 operation |
| 100 | 1 operation |
| 1000 | 1 operation |
Pattern observation: The time is constant regardless of input size.
Time Complexity: O(1)
This means the execution time is constant for this operation inside the module.
[X] Wrong: "Importing a module makes the program slower as the module size grows."
[OK] Correct: Importing just brings code in once; running functions inside the module costs time only when called.
Understanding module scope time helps you explain how code organization affects performance in real projects.
"What if the greet function called another function inside the module multiple times? How would the time complexity change?"
Practice
module scope mean in JavaScript?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]
- Thinking all variables are global
- Confusing module scope with block scope
- Believing functions must be inside loops
greet from a module?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]
- Placing export after function keyword
- Using incorrect export assignment syntax
- Missing parentheses in function declaration
const secret = 42; export const visible = 'hello'; console.log(typeof secret); console.log(typeof visible);
What is the output when this module runs?
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]
- Thinking private variables are undefined inside module
- Confusing export with variable existence
- Expecting ReferenceError inside same file
const count = 10; export count = 20;
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]
- Trying to export by assignment
- Missing const/let/var with export
- Assuming export can change variable value
// 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?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]
- Assuming private variables are imported automatically
- Confusing import syntax errors with runtime errors
- Expecting undefined instead of ReferenceError
Ready to prove it?
Pass the test and this pattern moves unknown
