JavaScript, inside out · Chapter 36 of 51

Modules

By the end of this chapter you can split code across files: share what a file exports, pass exports on, keep a shared value up to date, and load a file only when it is needed. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Modern JavaScript
  • 5 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Tapri's code has grown into one long file: the menu, the prices, the order counter and the receipts. When Ravi and Anjali both edit it on the same afternoon, one of them loses an hour. Kabir wants it split: menu.js owns the menu, exercise.js builds on it, and the monsoon specials, heavy with photos in the real app, get a file of their own that loads only when a customer opens them.

With what you know so far, everything lives in one file and every name can be read and changed from anywhere in it. A second file can share nothing with the first, and the specials load for every customer, whether they look at them or not. This chapter has three files: open them all from the file list.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    menu.js: priceOf and a default rupee formatter

    In menu.js, write priceOf(name), a named export. It returns the price of the menu item with exactly that name, or undefined when the item is not on the menu: priceOf('Masala chai') gives 40, priceOf('Filter coffee') gives undefined.

    Make formatRupees the default export of menu.js and write it: it returns ₹ followed by the amount with Indian digit grouping. 40 gives '₹40', 1250 gives '₹1,250' and 125000 gives '₹1,25,000'.

  2. Task 2

    exercise.js: re-export the menu helpers

    Make exercise.js re-export menu.js's helpers, so other files can import them from exercise.js: priceOf under its own name, and menu.js's default export under the name formatRupees. They must be the very same functions as in menu.js, not new ones that call them.

    (Task 4 adds ordersToday and recordOrder to the same re-export.)

  3. Task 3

    orderTotal and receiptLine: total an order with the menu helpers

    In exercise.js, write orderTotal(names). It adds up the prices of the named items using priceOf from menu.js: ['Masala chai', 'Bun maska'] gives 75, a name may appear more than once, and an empty list gives 0. When a name is not on the menu, throw an Error with the message 'Not on the menu: <name>', such as 'Not on the menu: Filter coffee'.

    Write receiptLine(names). It returns 'Total: ' followed by the order total formatted by menu.js's default export: 'Total: ₹75', and 'Total: ₹1,200' for 30 masala chai.

  4. Task 4 · interview question

    Interview: ordersToday, an order counter importers see change

    Fix the order counter in menu.js. ordersToday is a number every importer reads, and recordOrder() adds one order and returns the new count. After each recordOrder(), ordersToday must show the new count, both when imported from menu.js and when imported from exercise.js.

    It is broken now: importers see 0 forever. Also re-export ordersToday and recordOrder from exercise.js, so a recordOrder() called through exercise.js updates the same counter.

  5. Task 5

    loadSpecials: load the monsoon specials only when asked

    In exercise.js, write loadSpecials(). It loads specials.js with import('./specials.js') only when it is called, and returns a promise for the special names, each followed by the season in brackets, in order: ['Ginger chai (monsoon)', 'Onion pakoda (monsoon)', 'Corn on the cob (monsoon)'].

    exercise.js must not import or re-export specials.js at the top: the check reads the file and fails on such a line.

What you will learn

  • Named and default exports: what a file shares, and how another file imports it.
  • Re-exports: passing another file's exports on.
  • Live bindings: an import always shows the exporter's current value.
  • import(): loading a module only when it is asked for.

More in Modern JavaScript

Build "Modules" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.