export
In JavaScript, the export keyword makes a module’s functions, classes, and values available to other modules, which load them with import. Export declarations work only at the top level of a module.
A module can have any number of named exports. It can put export in front of a declaration, or list existing bindings in braces and rename them with as. It can also have one default export, marked with default, which importers receive under a name of their choice. Exports are live bindings, so when the exporting module reassigns a variable, importers see the new value.
Re-exports combine export with from to pass along another module’s exports without creating local bindings. The export * from form re-exports every named export but skips the default export, while export * as <name> from gathers all of the module’s exports, the default included, into one namespace object. Modules and export were added in ES2015, and the export * as <name> from form in ES2020.
JavaScript export Keyword Syntax
The export keyword appears in these forms:
export <declaration>;
export { <name> [as <exportedName>], ... };
export default <expression>;
export { <name> [as <exportedName>], ... } from "<specifier>";
export * from "<specifier>";
export * as <namespace> from "<specifier>";
JavaScript export Keyword Examples
The counter.mjs module exports a variable, a function, and a default function:
counter.mjs
export let count = 0;
export function increment() {
count += 1;
}
export default function reset() {
count = 0;
}
A second module, index.mjs, re-exports part of counter.mjs under new names:
index.mjs
export { count as clicks, increment as click } from "./counter.mjs";
The main program, main.mjs, imports from index.mjs and sees each change to the live binding:
main.mjs
import { clicks, click } from "./index.mjs";
console.log(`Before: ${clicks}`);
click();
click();
console.log(`After: ${clicks}`);
Running main.mjs prints the counter before and after the clicks:
$ node main.mjs
Before: 0
After: 2
The clicks binding still points to count in counter.mjs, so it reflects both increments.
JavaScript export Keyword Use Cases
- Sharing functions, classes, and constants between modules
- Marking a module’s main value with
export default - Collecting several modules into one entry point with re-exports
- Renaming exports to give a module a cleaner public API
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 4, 2026