imports and exports javascript.info 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: 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';

Tags:

Misc Example