Intermediate · 12 min
Organize code with modules
Import named exports from a supporting file.
./math.js
export function add(a, b) { return a + b; }
export function multiply(a, b) { return a * b; }Export and import
A module can export reusable values and functions. A named import must match an exported name. The supporting file shown here is available locally in the playground.
import { add } from "./math.js";
console.log(add(3, 4));Output
7
Rename an import
Use as to choose a local name for a named import. The original export remains unchanged. Modules have their own scope and run in strict mode.
import { multiply as product } from "./math.js";
console.log(product(3, 5));Output
15
Put it into practice
- Predict each result before running the example.
- Complete the coding task, then explain why your solution works.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Use the correct export
Import multiply from the supporting module and use it to calculate total for 4 and 3. Display total.
Change both the imported name and the function call to multiply.