The export
statement is used when creating JavaScript modules to export functions, objects, or primitive values from the module so they can be used by other programs with the import
statement.
This feature is only implemented natively in Safari and Chrome at this time. It is implemented in many transpilers, such as the Traceur Compiler, Babel or Rollup.
export { name1, name2, …, nameN }; export { variable1 as name1, variable2 as name2, …, nameN }; export let name1, name2, …, nameN; // also var, function export let name1 = …, name2 = …, …, nameN; // also var, const export default expression; export default function (…) { … } // also class, function* export default function name1(…) { … } // also class, function* export { name1 as default, … }; export * from …; export { name1, name2, …, nameN } from …; export { import1 as name1, import2 as name2, …, nameN } from …;
nameN
import
in another script).There are two different types of export. Each type corresponds to one of the above syntax:
// exports a function declared earlier export { myFunction }; // exports a constant export const foo = Math.sqrt(2);
export default function() {}
export default class {}
Named exports are useful to export several values. During the import, it is mandatory to use the same name of the corresponding object.
But a default export can be imported with any name for example:
export default k = 12; // in file test.js import m from './test' // note that we got the freedom to use import m instead of import k, because k was default export console.log(m); // will log 12
There can be only one default export
The following syntax does not export a default export from the imported module:
export * from …;
If you need to export the default, write the following instead:
import mod from 'mod'; export default mod;
In the module, we could use the following code:
// module "my-module.js" function cube(x) { return x * x * x; } const foo = Math.PI + Math.SQRT2; export { cube, foo };
This way, in another script, we could have:
import { cube, foo } from 'my-module'; console.log(cube(3)); // 27 console.log(foo); // 4.555806215962888
If we want to export a single value or to have a fallback value for our module, we could use a default export:
// module "my-module.js" export default function cube(x) { return x * x * x; }
Then, in another script, it will be straightforward to import the default export:
import cube from 'my-module'; console.log(cube(3)); // 27
Note that it is not possible to use var
, let
or const
with export default
.
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Exports' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Exports' in that specification. | Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 61 |
16 151 |
542 | No | 47 | 10.1 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | No | 61 | Yes | 542 | 47 | 10.1 | ? |
1. From version 15: this feature is behind the Experimental JavaScript Features
preference.
2. From version 54: this feature is behind the dom.moduleScripts.enabled
preference. To change preferences in Firefox, visit about:config.
import
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export