Skip to content
dreamcode
dreamcode
Map
ES Modules
Lesson 34 of 48
+15 XP on finish
JS MODULESChapter 6 · JS Async and Errors

ES Modules

Real programs are split into many files called modules. Each module has its own scope: nothing inside it is visible to other files unless it is exported. export const add = ... creates a named export, imported with braces: import { add } from "./math.js". A file can also have one default export, imported without braces and under any name you like. This lesson is read and quiz, because modules are about several files working together.

Worked example
// file: math.js
export const add = (a, b) => a + b;

export default function square(n) {
  return n * n;
}

// file: main.js
import square, { add } from "./math.js";

console.log(add(2, 3), square(4));

How it reads

  • export const add is a named export: import it by its exact name in braces
  • export default marks the file's main value: import it without braces
  • Everything not exported stays private to its file
Cloud tip: Prefer named exports in shared code: editors can find them, and a typo in an import fails loudly instead of silently.

Check your understanding

0 / 3

Answer all 3 to complete this lesson and earn 15 XP.

  1. 1. How does main.js get the default export of math.js?
  2. 2. What does export const add = (a, b) => a + b; create?
  3. 3. What happens to a helper function in math.js that is not exported?
Answer every question to unlock the next lesson.