Need to share a function across an application without putting it on the global object? Put it in a module and export it. Other code can import what it needs; declarations you do not export stay inside the module. section 20.2
Share a function, keep its helper private
In stats.js: function statsHelper(stat) { // process the stat } export default function logStats(stat) { statsHelper(stat) // log the stat } Another module can write `import recordStat from './stats.js'`. The importing name can differ from `logStats` because this is a default export. `statsHelper` remains private. A module can have only one default export. section 20.2 section 21.6
Choose the form that matches what you want to expose:
- `export function currency(num) { /* ... */ }` creates a named export. You can instead declare the function first and write `export { currency }`. A module can have several named exports, and a brace-list export can give one an alternate name. section 20.2 section 20.4
- Import a named export with its exported name: `import { currency } from './format.js'`. You can rename it with `as`, or import all named values with `*`. Named imports are direct bindings to exported variables; imports are read-only. section 21.6
- Keep export statements at the module’s top level, not inside an `if` block or function. Values can also be exported directly from another module. section 20.4 section 21.6
Export a class
`export default class DataStore {}` pairs with `import Store from './data_store'`. Change the declaration to `export class DataStore {}` and the import becomes `import { DataStore } from './data_store'`. The first form lets the importer choose a name; the second requires the exported name. section 27.3
Quick practice: create a module with an unexported `luckyNumber` and a default-exported `guessLuckyNumber(guess)` that returns whether the guess matches. Then import the function in another module. section 20.4 section 21.6




