In addition to named imports, you can provide a default export.
// circle.js
export const PI = 3.14;
export default function area(radius) {
return PI * radius * radius;
}
You can use a simplified syntax to import the default export.
import circleArea from './circle';
console.log(circleArea(4));
Note that a default export is implicitly equivalent to a named export with the name default
, and the imported binding (circleArea
above) is simply an alias. The previous module can be written like
import { default as circleArea } from './circle';
console.log(circleArea(4));
You can only have one default export per module. The name of the default export can be omitted.
// named export: must have a name
export const PI = 3.14;
// default export: name is not required
export default function (radius) {
return PI * radius * radius;
}