ES Modules: import & export in JavaScript
ES modules organize code into files with explicit interfaces: named exports (export const x, export function f) imported selectively (import { x } from "./a.js"), default exports (export default ...) for single main values, and namespace/side-effect forms (import * as ns, import "./setup"). Bindings are live read-only views β exporters updating let reflects in importers.
Modules are strict, file-scoped (no globals), and statically analyzable (enabling tree-shaking) β with URLs/paths as specifiers (extensions required in browsers and Node ESM). Circular imports work through hoisted live bindings when structured carefully.
Export named values
export const x = 1; export function f() {} β the module's public surface, statically visible.
Import selectively
import { x, f } from "./a.js" β names must match (or alias via as); extensions required outside bundlers.
Default for mains
export default Component; imported as any name β one primary export per module, by convention.
Live read-only bindings
Imported lets reflect exporter updates; importers can't reassign β shared live state without shared mutability.
// math.js: export function add(a, b) { return a + b; }// app.js: import { add } from "./math.js";// console.log(add(2, 3));Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself β building beats watching.