import / export (JavaScript)
import and export are the keywords that let JavaScript files share code as modules. export makes a value available, and import brings it into another file.
JavaScript
Modules keep code organized by splitting it into files that expose only what they choose. You can have named exports (any number) and one default export per file.
Browsers load these with <script type="module">, and bundlers and Node also support them.
Syntax
export const x = ...; import { x } from "./file.js";Examples
JavaScript
// math.js
export const add = (a, b) => a + b;
// app.js
import { add } from "./math.js";
console.log(add(2, 3));Output:
5