liveBook · Manning

Named export

As explained in Secrets of the JavaScript Ninja, Second Edition

An exported module binding identified by its declared or explicitly assigned name, allowing a module to expose multiple bindings.

Expose several useful bindings without opening up every top-level variable: a named export makes a module’s selected variables or functions available to importing code. section 11.2.1

Export and import by name:

  • Mark declarations with export, or export existing bindings together, such as export { message, sayHiToNinja }. section 11.2.1
  • Import selected names with braces; use aliases to expose a binding under another name, or import * as a namespace for all named exports. section 11.2.1
  • Only exported bindings are accessible from outside the module; an unexported variable remains private to it. section 11.2.1
Listing 11.5. Exporting from a Ninja.js module
const ninja = "Yoshi";                    #A
export const message = "Hello";           #B
                                          #B
export function sayHiToNinja() {          #B
  return message + " " + ninja;           #C
}

A small module exports a message and greeting function while keeping its ninja variable unexported. section 11.2.1

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Shows declaration and export-list syntax, named imports, aliases, and namespace imports without exploring runtime internals.
Examples
Uses a Ninja module that exports a greeting message and function while keeping its ninja variable private.
What you can do after
Apply named-export syntax to expose selected bindings and import them by name or through a module namespace.
How it connects
Connects named exports to JavaScript modules, import aliases, namespace imports, and default exports.

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 Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.