Default Exports
A module can have one default export. The importer can name it anything. Default exports are common for classes and the main export of a module.
A module can have one default export. The importer can name it anything. Default exports are common for classes and the main export of a module.
// Logger.js
export default class Logger {
constructor(prefix) {
this.prefix = prefix;
}
log(msg) {
console.log(`[${this.prefix}] ${msg}`);
}
}
// app.js
import Logger from "./Logger.js"; // Named anything
import AppLogger from "./Logger.js"; // Same module, different name
const log = new Logger("App");
log.log("Started");
A module can have both a default export and named exports — combine them when there is one primary export plus helpers.
More in JavaScript