Created
June 10, 2021 16:50
-
-
Save kevinhooke/895723ca2991f837785c503916e064f4 to your computer and use it in GitHub Desktop.
JavaScript imports and exports: default and named
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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