liveBook · Manning

Named export

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

A module export that is made available under an explicit name and must be imported by that name unless renamed.

Need a formatting module to provide both currency and number formatting? A named export makes each value available under a specific name. Unlike a default export, which an importer can name freely, a named export must be requested by its exported name unless the import renames it. section 20.2 section 21.2

Export two functions

export function currency(num) { // Return a formatted currency string } export function number(num) { // Return a number formatted with commas } Each `export` prefixes a declaration, so the declared function name becomes its exported name. The module can have several named exports, but only one default export. section 20.2

You can also declare values first and list their existing bindings afterward: `export { currency, number }`. To give a binding a different public name, use `export { originalName as exportedName }`. Braces require bindings, not raw values; export statements must be at the module’s top level. section 20.2 section 20.4

import { currency, number } from './utils/format'

function details(product) {
  return `
    price: ${currency(product.price)}
    ${number(product.quantityAvailable)} in stock ready to ship.
  `
}

The product-details function imports `currency` and `number` by their exported names, then calls each where its formatting is needed. section 21.2

If an exported name conflicts with another import, rename it locally: `import { number as formatter } from './utils/format'`. The export is still named `number`; only this module’s local binding is `formatter`. You can import all named exports with `import * as format`, but ordinarily import only what you use. section 21.2

Quick practice

Create a module with two formatting functions. Export both by name, import just one into another module, then rename that import with `as`. Check that the name inside the export braces matches the declared binding. section 20.2 section 21.2

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

How deep it goes
Explains declaration and brace-list syntax, export aliases, matching imports, and top-level placement without descending into module internals.
Examples
Shows currency and number formatters, product-detail formatting, and aliases that avoid conflicting import names.
What it assumes
Builds on function and variable declarations while introducing named export and import syntax directly.
What you can do after
Build it from scratch by exporting multiple declarations, importing selected names, and renaming an import when needed.
How it connects
Contrasts named exports with default exports and connects them to named imports, import aliases, and namespace imports.

Also explained in

Compare how 2 books explain named 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.