using import and export in javascript code example
Example 1: js import and export
// helloworld.js
export function helloWorld() {
return 'Hello World!';
}
// main.js
import helloWorld from './helloworld.js';
console.log(helloWorld());
Example 2: import and export type in js
// Exporting individual features
export let name1, name2, …, nameN; // also var, const
export let name1 = …, name2 = …, …, nameN; // also var, const
export function functionName(){...}
export class ClassName {...}
// Export list
export { name1, name2, …, nameN };
// Renaming exports
export { variable1 as name1, variable2 as name2, …, nameN };
// Exporting destructured assignments with renaming
export const { name1, name2: bar } = o;
// Default exports
export default expression;
export default function (…) { … } // also class, function*
export default function name1(…) { … } // also class, function*
export { name1 as default, … };
// Aggregating modules
export * from …; // does not set the default export
export * as name1 from …; // Draft ECMAScript® 2O21
export { name1, name2, …, nameN } from …;
export { import1 as name1, import2 as name2, …, nameN } from …;
export { default } from …;
Example 3: different types ways of export and import in javascript
// Import a module without any import bindings, just to
// execute its code without assigning any variables here.
import 'example';
// Import the default export of a module.
import exampleDefaultExport from 'example';
// Import a named export of a module.
import { property } from 'example';
// Import a named export to a different name,
import { property as exampleProperty } from 'example';
// Import all exports from a module as properties of an object.
import * as example from 'example';
// Export a named variable.
export var property = 'example property';
// Export a named function.
export function property() {};
// Export an entity to the default export.
export default 'example default';
// Export an existing variable.
var property = 'example property';
export { property };
// Export an existing variable as a new name.
export { property as exampleProperty };
// Export an export from another module.
export { property as exampleProperty } from 'example';
// Export all exports from another module.
export * from 'example';