liveBook · Manning

Import/export syntax

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

A module syntax form that makes a declaration or value available outside the current module.

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

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

How deep it goes
Works through default and named forms, import naming, read-only bindings, and top-level export placement without detailing module loading.
Examples
Uses a statistics logger with a private helper, formatting functions, class imports, and a lucky-number module exercise.
What it assumes
Assumes familiarity with JavaScript functions and declarations while introducing module sharing through concrete examples.
What you can do after
Build it from scratch: create a module with private state, export its public function, and import that function elsewhere.
How it connects
Connects module scope and private declarations with default exports, named exports, direct bindings, and classes.

Also explained in

Compare how 5 books explain import/export syntax →

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.