Sub topic 3
Default Exports and When to Use Them
This chapter assumes you already know named exports from the previous chapter, and core JavaScript functions, classes, and objects. Importing is only shown briefly here. The next chapter covers it in full.
What a default export is
A default export is the one value a module offers as its main thing. Each module can have at most one. When importers load it, they choose the local name themselves.
// logger.js
export default function log(message) {
console.log(`[app] ${message}`);
}
export const level = 'info';// main.js
import log, { level } from './logger.js';
log(`level=${level}`); // [app] level=infoThe importer could have written import write from './logger.js' and it would work the same way. The name log in logger.js doesn't affect what callers call it.
The forms of `export default`
You can export a declaration, an expression, or an existing binding.
// Pick ONE of these per module; each is shown here for comparison.
// 1. Function or class declaration (the name is optional)
export default class Cart {
constructor() { this.items = []; }
}
// 2. Any expression
// export default { currency: 'EUR', taxRate: 0.2 };
// 3. An existing binding, through an export list
// const createCart = () => new Cart();
// export { createCart as default };The third form shows how defaults work under the hood. A default export is a named export whose name is default. That is why import { default as Cart } from './cart.js' also works.
A gotcha: expressions are evaluated once
export default someVariable; exports the value that the expression had when it ran. It doesn't export the variable itself. Later reassignments of someVariable are not seen by importers. The forms export default function, export default class, and export { x as default } export the binding itself. The chapter on live bindings explains why this matters.
Default or named?
| Situation | Better choice |
|---|---|
| Module has one main component, class, or function | Default is acceptable |
| Module offers several related utilities | Named exports |
| You want the same name used across the codebase | Named exports |
| A framework or tool requires a default (for example, some routers or lazy loaders) | Default |
Named exports have practical advantages. Editors can auto-import them by name, typos fail at link time with a clear error, and renaming them in a refactor updates every import. With defaults, each file can call the same value by a different name, which makes the codebase harder to search. Many teams use named exports everywhere unless a tool requires a default.
Exercise
A module contains let theme = 'light'; export default theme; theme = 'dark';. What value does import theme from './theme.js' give, and how would you change the export so importers see 'dark'?
Show answer
It gives 'light', because export default theme evaluates the expression once and exports that value. Write export { theme as default }; instead. That exports the binding itself, so importers see the current value, 'dark'.
Exercise
Why does import { default as Cart } from './cart.js' work?
Show answer
A default export is a named export whose name is default, so you can import it by that name and rename it.