Need to share one reusable function across an application without making it global? Put it in a module and export it as that module’s default value. A module can have only one default export, though it can also have named exports. section 20.2
Write `export default <expression>` to export the expression’s value. For example, `export default 5` exports a number, while `export default function currency(num) { /* format currency */ }` exports a function. Other declarations can stay private to the module. section 20.2
function statsHelper() {
// do some stat processing
}
export default function logStats(stat) {
// log stat to data base using statsHelper
}
The exported `logStats` function can use `statsHelper`, but the helper is not exported. No global function is needed. section 20.2
import formatCurrency from './utils/format/currency' 1
function price(num) {
return formatCurrency(num)
}
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":63},{\"line\":0,\"ch\":64}]]"}
!@%STYLE%@!
The module exports a function named `currency`, but the importer calls it `formatCurrency`. A default import chooses its own local name; it does not need to match the exported function’s name. section 21.2
Compare named exports: `import { currency, number } from './utils/format'` requests exports by name. A named import can use `as` to choose a different local name. A default import needs no braces, and one import statement can bring in both default and named exports. section 21.2
export default function ajax(url) {
// perform ajax
}
export function setAjaxDefaults(options) {
// store options
}
One module can expose its main `ajax` function as the default and `setAjaxDefaults` as a named export. “Default” means primary, not only. section 20.2
A class can be the default too: `export default class DataStore { /* class body */ }` can be imported as `Store`. If exported by name instead, it is imported as `{ DataStore }`. One class per module is a common reason to choose a default export. section 27.3
Try it
Create `luck_numbery.js` with a private `luckyNumber`. Default-export `guessLuckyNumber(guess)` so callers can check whether a guess matches without importing the hidden variable. section 20.4


