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 addis a named export: import it by its exact name in bracesexport defaultmarks 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.


