liveBook · Manning

Default export

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

The single export in a module that is treated as the module's primary exported value and imported without curly-brace name matching.

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

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Explains export expressions, the one-default rule, local import naming, and contrasts with named exports; does not explore module loading internals.
Examples
Shows statistics logging, currency formatting, and a class export; assigns a private-number guessing module as practice.
What it assumes
Assumes basic JavaScript functions and variables while introducing module scope and export/import syntax through examples.
What you can do after
Build it from scratch by default-exporting a module’s main function and importing it under a chosen local name.
How it connects
Connects default exports to module scope, named exports, import aliases, and class exports.

Also explained in

Compare how 3 books explain default export →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.