Lesson 1
Modules
Build a practical mental model for modules and use it in working JavaScript.
By the end, you will be able to…
- Explain modules in your own words
- Use modules in a small program
- Recognize and debug a common failure mode
Build the mental model
Modules let you split code into files with an explicit public API (export) while keeping implementation details private by default. import brings only what you name into another file's scope.
Syntax and working example
Read the code from inputs to output. Predict what it prints, then run it and change one value.
Why this matters
Modules are what makes large codebases navigable — each file has one clear responsibility and a small, deliberate surface area other files depend on.
Real-world example
Every npm package you'll ever install is a module (or set of modules) exporting a small, documented public API.
Common mistakes
- Exporting everything by default instead of a deliberate, minimal API
- Creating circular imports between two modules that need each other
- Confusing default exports (one per file) with named exports (many per file)
Try it yourself
Challenge
Given a module-style object, call its exported function correctly.
Knowledge check
What is the main benefit of splitting code into modules?
Summary
Split code into modules with a small, explicit export surface; import only what each file needs.