📖 Scenario: You are building a small JavaScript module to manage a user's profile data. You want to keep some data private inside the module and expose only what is necessary.
🎯 Goal: Create a JavaScript module that stores a user's name privately and exposes a function to get the user's name.
📋 What You'll Learn
Create a module using an immediately invoked function expression (IIFE).
Inside the module, create a private variable called userName with the value 'Alice'.
Add a function called getUserName inside the module that returns the value of userName.
Expose only the getUserName function to the outside of the module.
Print the result of calling getUserName() to the console.
💡 Why This Matters
🌍 Real World
Modules help keep code organized and protect data from being changed accidentally by other parts of a program.
💼 Career
Understanding module scope is important for writing clean, maintainable JavaScript code in real projects and working with libraries.
Progress0 / 4 steps
1
Create the module and private variable
Create a module using an immediately invoked function expression (IIFE). Inside it, create a private variable called userName and set it to 'Alice'.
Javascript
Hint
Use (function() { ... })(); to create a module. Declare const userName = 'Alice' inside it.
2
Add a function to get the user name
Inside the module, add a function called getUserName that returns the value of userName.
Javascript
Hint
Define function getUserName() { return userName; } inside the module.
3
Expose the getUserName function
Modify the module to expose only the getUserName function to the outside by returning an object with getUserName as a property.
Javascript
Hint
Return an object with getUserName inside the IIFE and assign it to userModule.
4
Print the user name using the module
Use console.log to print the result of calling userModule.getUserName().
Javascript
Hint
Call console.log(userModule.getUserName()) to show the user name.
Practice
(1/5)
1. What does module scope mean in JavaScript?
easy
A. All variables are global and accessible everywhere
B. Variables and functions are private to the file unless exported
C. Functions can only be used inside loops
D. Variables must be declared with var only
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 B
Quick 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
A. export function greet() {}
B. function export greet() {}
C. export greet() {}
D. export = greet()
Solution
Step 1: Recall correct export syntax
To export a function, use export 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 A
Quick Check:
Export function syntax = export function name() [OK]
Hint: Use 'export function' before function name [OK]