Skip to content

Instantly share code, notes, and snippets.

@ahcode0919
Last active September 11, 2019 23:21
Show Gist options
  • Save ahcode0919/9a8faf2cc851626e51c012a3aac707dc to your computer and use it in GitHub Desktop.
Save ahcode0919/9a8faf2cc851626e51c012a3aac707dc to your computer and use it in GitHub Desktop.
ES6 Import Export

ES6 Import / Export

Named Exports / Imports

Exports

// source.js
export function add(a, b) { return a + b; }
export function subtract(a, b) { return a - b; }

Imports

// main.js
import { add } from 'source.js';
add(1, 1); // 2

// specify multiple
import { add, subtract } from 'source.js';

// import all
import * as math from 'source.js';
math.add(1, 1); // 2

Default Exports / Imports

One default export is allowed per file

Exports

// default_source.js
export default function(a, b) { return a * b; }

Imports

// main.js
import multiply from './default_source.js';
multiply(1, 2); // 2

Mixed Exports / Imports

Exports

// mixed_default_source.js
// Default export (one per file)
export default function(a, b) { return a * b; }

export function add(a,b) { return a + b; }

Imports

// main.js
import defaultFunctionName, { add } from './mixed_default_source.js';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment