

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Modules
incomplete
2: CommonJS
incomplete
3: Strict Mode
incomplete
4: ES6 Modules
incomplete
5: Node.js Modules
incomplete
6: Browser Modules
incomplete
7: Default Exports
incomplete
8: Bundlers
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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 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:
// math.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
// main.js
import { add, subtract } from "./math.js";
console.log(add(1, 2)); // 3
console.log(subtract(1, 2)); // -1