← JavaScript Essentials
Modern JavaScript and projects · 15 MIN

Modules and reusable code

Modules and reusable code

ES modules use export and import to share functionality. A module has its own scope and is loaded with type="module" in a browser. Organize code around features and avoid circular dependencies.

Try it yourself

Change the example, run it, and explain the result in your own words. Then create one variation of your own.

Key idea

Use the example as a starting point, not something to memorize. Practise, inspect the result and make small improvements.

Code example

// math.js
export const square = n => n * n;
// app.js
import { square } from "./math.js";
console.log(square(5));