1// helloworld.js
2
3export function helloWorld() {
4 return 'Hello World!';
5}
6
7// main.js
8
9import helloWorld from './helloworld.js';
10
11console.log(helloWorld());
1// Exporting individual features
2export let name1, name2, …, nameN; // also var, const
3export let name1 = …, name2 = …, …, nameN; // also var, const
4export function functionName(){...}
5export class ClassName {...}
6
7// Export list
8export { name1, name2, …, nameN };
9
10// Renaming exports
11export { variable1 as name1, variable2 as name2, …, nameN };
12
13// Exporting destructured assignments with renaming
14export const { name1, name2: bar } = o;
15
16// Default exports
17export default expression;
18export default function (…) { … } // also class, function*
19export default function name1(…) { … } // also class, function*
20export { name1 as default, … };
21
22// Aggregating modules
23export * from …; // does not set the default export
24export * as name1 from …; // Draft ECMAScript® 2O21
25export { name1, name2, …, nameN } from …;
26export { import1 as name1, import2 as name2, …, nameN } from …;
27export { default } from …;
1// Import a module without any import bindings, just to
2// execute its code without assigning any variables here.
3import 'example';
4
5// Import the default export of a module.
6import exampleDefaultExport from 'example';
7
8// Import a named export of a module.
9import { property } from 'example';
10
11// Import a named export to a different name,
12import { property as exampleProperty } from 'example';
13
14// Import all exports from a module as properties of an object.
15import * as example from 'example';
16
17// Export a named variable.
18export var property = 'example property';
19
20// Export a named function.
21export function property() {};
22
23// Export an entity to the default export.
24export default 'example default';
25
26// Export an existing variable.
27var property = 'example property';
28export { property };
29
30// Export an existing variable as a new name.
31export { property as exampleProperty };
32
33// Export an export from another module.
34export { property as exampleProperty } from 'example';
35
36// Export all exports from another module.
37export * from 'example';
1// Exporting individual features
2export let name1, name2, …, nameN; // also var, const
3export let name1 = …, name2 = …, …, nameN; // also var, const
4export function functionName(){...}
5export class ClassName {...}
6
7// Export list
8export { name1, name2, …, nameN };
9
10// Renaming exports
11export { variable1 as name1, variable2 as name2, …, nameN };
12
13// Exporting destructured assignments with renaming
14export const { name1, name2: bar } = o;
15
16// Default exports
17export default expression;
18export default function (…) { … } // also class, function*
19export default function name1(…) { … } // also class, function*
20export { name1 as default, … };
21
22// Aggregating modules
23export * from …; // does not set the default export
24export * as name1 from …;
25export { name1, name2, …, nameN } from …;
26export { import1 as name1, import2 as name2, …, nameN } from …;
27export { default } from …;
1// helloworld.js
2
3export function helloWorld() {
4 return 'Hello World!';
5}
6
7// main.js
8
9import helloWorld from './helloworld.js';
10
11console.log(helloWorld());
12