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// Three different export styles
2export foo;
3export default foo;
4export = foo;
5
6// The three matching import styles
7import {foo} from 'blah';
8import foo from 'blah';
9import * as foo from 'blah';
10
1import React from 'react'; // get the React object from the react module
2
3class HelloWorld extends React.Component {
4 render() {
5 return <p>Hello, world!</p>;
6 }
7}
8
9export default HelloWorld; // expose the HelloWorld component to other modules
1/*
2Since export default is used to declare a fallback value for a module or
3file, you can only have one value be a default export in each module or
4file. Additionally, you cannot use export default with var, let, or const.
5*/
6
7// named function
8export default function add(x, y) {
9 return x + y;
10}
11
12// anonymous function
13export default function(x, y) {
14 return x + y;
15}
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// Default exports
2export default expression;
3export default function (…) { … } // also class, function*
4export default function name1(…) { … } // also class, function*
5export { name1 as default, … };