We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

ES6 Modules

ES6 modules are the preferred way of doing modules in JavaScript. They're (now) built into the language and are supported in modern browsers and Node.js. That said, there are still some gotchas to be aware of... which we'll cover.

The Syntax

The syntax for ES6 modules is drop-dead simple. You can export stuff from a module using the export keyword, and import stuff using the import keyword. Let's look at our math example again:

Exporting

// math.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;

Importing

// main.js
import { add, subtract } from "./math.js";

console.log(add(1, 2)); // 3
console.log(subtract(1, 2)); // -1