JavaScript Reference ยท Statements and Declarations
The export declaration
Reference for the JavaScript export declaration: named exports, default exports, export lists, renaming, re-exporting from other modules and live bindings.
The export declaration makes values from a module available to other modules. Everything not exported stays private to the module. A module can have any number of named exports and at most one default export.
Exports are bindings, not copies: if an exported let variable changes inside the module, importers see the new value.
export const VERSION = "2.1"; export function add(a, b) { return a + b; } export default class Cart {}
Try it
Modules normally live in separate files. The demo below creates one from a data: URL so you can see what each form exports.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
export const x = 1; // declarations export function f() {} export class C {} export { x, f as renamed }; // list export default expression; // default export default function () {} export { name } from "other.js"; // re-export export * from "other.js"; export * as ns from "other.js"; export { default } from "other.js"; export { default as name } from "other.js";
Usage notes
- Top level only.
exportcannot appear inside functions or blocks. - Default export names. A default export has no fixed name; each importer picks one. Named exports must be imported by their exact names (or renamed with
as). export *skips the default export and cannot silently shadow local names; conflicting names from twoexport *lines become ambiguous and are excluded.- Live bindings. Importers see updates, but only the exporting module can assign to the variable.
- Barrel files that re-export many modules are convenient but can defeat tree-shaking in some bundlers.
- Exporting objects shares the same object; consumers can mutate it.
- Prefer named exports for discoverability, editor auto-import and refactoring. Use default for a module that is "one thing".
- Order.
exportstatements are hoisted; their position in the file does not matter.
Browser support
Exports work in all current browsers when the script is loaded as a module, and in Node.js ES modules. export * as ns is supported by current browsers; check a compatibility table for older targets.