Start here.
As a program grows, keeping every name in one file becomes hard to manage. A module is a file with its own scope and an explicit way to share selected values. export makes a binding available to other modules, and import names what another file provides. For example, one file may export a function called total; another may import it and call total(items). The import is a dependency: this file relies on that other file's promise.
A named export and a default export have different import syntax. Named exports use braces, such as import { total } from './math.js';. The path tells the environment where to find the module. Browser modules normally use a script tag with type='module'; Node.js also supports modules with its own file and package rules. A module is evaluated once for a given module identity, then its exports can be used by importers.
A good module boundary keeps related behavior together and exposes a small, understandable surface. You do not need a separate file for every function. Think about what another part of the program should be allowed to know: a formatting function may be public, while its helper data can stay private. The boundary helps readers follow where a value came from and which file owns its meaning.
Build the model.
An ES module has its own scope. export chooses which bindings other modules may use; import names the dependencies a module needs. This helps a codebase grow without putting every name into one shared global space.
Imported bindings are live views of the exported bindings, not ordinary copied variables. Modules are loaded and linked before their bodies run. Cyclic imports can work, but they often make initialization order difficult to explain; a simpler dependency direction is usually easier to maintain.
How it works.
In a module, top-level names are local to that file unless exported. A named export such as export const double makes a binding available to importing modules. The importer writes both the binding name and the file path, which makes the dependency easy to locate. Browsers and Node.js support ES modules, although their file resolution rules and host APIs differ.
A module's public surface should be smaller than its private implementation. If every helper is exported, callers can become dependent on details you intended to change. Imports are live bindings: if an exported let value changes, importers see the current value, but they cannot assign to the imported name. Circular imports may introduce initialization surprises, so organize dependencies in a direction a reader can draw.
Read it line by line.
// math.js
export const double = n => n * 2;
// page.js
import { double } from './math.js';First notice. The file boundary tells readers where double comes from.
math.js declares and exports double. page.js imports that named binding from the relative path, so it can call double in its own code. The module loader links the two files before page.js executes. The import is not text pasted into page.js; it is a relationship between modules and their exported bindings.
See the rule again.
// helpers.js
const privateFactor = 2;
export const double = n => n * privateFactor;
// consumer.js
import { double } from './helpers.js';
console.log(double(4));The consumer can import double and would print 8. It cannot import privateFactor by name because that binding was not exported. The function can still use privateFactor internally; the module boundary hides the implementation detail from callers.
One thing to remember.
A module is a scope and a dependency contract.