×

javascript · level 3 · unit 4 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Named exports keep their names; a takes any name.

A module can share many named exports, like export const rate. Importing files must use those exact names: import { add, rate } from "./maths.js". Clash with a name you already have? Give it an : import { add as plus }.

A module can also have one default export, its main thing: export default class Cart. Importing files name it whatever they like: import ShoppingCart from "./cart.js". Our stand-in mirrors this with destructuring, where add: plus gives the alias.

const maths = (() => {
  const add = (a, b) => a + b;
  const rate = 0.2;
  return { add, rate };
})();
const { add: plus, rate } = maths;
const add = "a name already taken";
console.log(plus(2, 3), rate, add);

import * as maths from "./maths.js" gathers every named export into one object, much like this stand-in.