Understanding Module Scope in JavaScript
📖 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