Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinhooke/895723ca2991f837785c503916e064f4 to your computer and use it in GitHub Desktop.
Save kevinhooke/895723ca2991f837785c503916e064f4 to your computer and use it in GitHub Desktop.
JavaScript imports and exports: default and named
- a module can only have one default export:
export default A = //something
... and can be imported with any name:
import A from './A.js';
import B from './A.js';
import someothername from './A.js'
- named exports are imported by their exact name only, a module can have zero, one or more named exports
export const A = 1;
export const B = 2;
... these must be imported by name:
import { A } from './A.js';
import { B } from './A.js';
... this would fail if there's no named export called C
import { C } from './A.js';
... you can't change the name if there is no default export
import somethingelse from './A.js'; // this doesn't work
If there is a mix of default and named exports, you can import both styles at the same time
import A, {B, C} from '.A.js'; // assuming there is a default and B and C are named exports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment