type="module"
Loads a script as an ES module.
JavaScript modules split code into files with explicit dependencies instead of sharing everything through the global scope.
<script type="module" src="/assets/js/app.js"></script>
// app.js
import { formatTitle } from "./format.js";
Modules & Code Organization
A module is a JavaScript file that can import values from other modules and export values for other modules to use.
Module scripts use type="module". They run in strict mode, have their own scope and load dependencies through import statements.
The main value of modules is organization. You can keep data logic, DOM logic, formatting helpers and API code in separate files.
Loads a script as an ES module.
Top-level declarations do not become window globals.
Modules run with strict JavaScript rules automatically.
Imports describe which files depend on which other files.
Examples
import { loadItems } from "./api.js";
import { renderItems } from "./render.js";
const items = await loadItems();
renderItems(items);
window.items = [];
window.loadItems = function () {};
window.renderItems = function () {};
Code patterns
These examples focus on imports, exports, module boundaries, on-demand loading and file structure you can use in real projects.
Use type="module" in HTML.
<script type="module" src="/assets/js/app.js"></script>
Module paths are explicit.
import { renderList } from "./render-list.js";
Top-level values stay inside the file unless exported.
const cache = new Map();
export function getCached(key) { return cache.get(key); }
Module scripts do not block HTML parsing like classic scripts.
<script type="module" src="/assets/js/app.js"></script>
Rules that matter
A module system only helps when files have clear jobs, small public APIs and predictable import paths.
They keep code organized as files grow.
A file should show its dependencies at the top.
Modules already provide file-level scope.
API, DOM, formatting and state can live in separate files.
Browser imports need correct file paths.
Modules catch sloppy JavaScript patterns earlier.
Production thinking
Without modules, larger JavaScript projects become a chain of globals and hidden dependencies.
Organized modules make it easier to keep interaction, focus handling and status messages consistent across a site.
Production builds often bundle modules, but the source code should still be structured as modules.
Module organization does not rank pages directly, but reliable JavaScript helps rendered content and navigation work consistently.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain what a file exports, what it imports and why it exists, the module structure is doing its job.
Senior audit upgrade
Use these references when browser support, syntax details or proposal status matters.