Learn JavaScript
❯ errors_modules.es_modules

ES Modules: import & export in JavaScript

$concept

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.

$how it works
1

Export named values

export const x = 1; export function f() {} β€” the module's public surface, statically visible.

2

Import selectively

import { x, f } from "./a.js" β€” names must match (or alias via as); extensions required outside bundlers.

3

Default for mains

export default Component; imported as any name β€” one primary export per module, by convention.

4

Live read-only bindings

Imported lets reflect exporter updates; importers can't reassign β€” shared live state without shared mutability.

$example
// math.js: export function add(a, b) { return a + b; }
// app.js: import { add } from "./math.js";
// console.log(add(2, 3));
Output
5
1math.js exports add as a named binding β€” its public interface.
2app.js imports it by exact name with the extension-bearing path.
35 prints across the module boundary β€” shared code with explicit interfaces.
$use cases
File-scoped code with explicit import/export interfaces
Tree-shakeable libraries via static named exports
Default exports for single-purpose modules
Live bindings for shared-but-controlled state

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself β€” building beats watching.