ES Modules: import and export
ES Modules, standardized in ES2015 and now natively supported by every modern browser and Node.js, let you split code into files with explicit imports and exports.
// math.js
export function add(a, b) { return a + b; }
export default function multiply(a, b) { return a * b; }
// main.js
import multiply, { add } from "./math.js";
<script type="module" src="main.js"></script>
A file can have any number of named exports (export function add) but only one default export (export default). Named exports are imported with matching curly-brace names ({ add }), while a default export can be imported under any name you choose (multiply here, but it could be called anything).
Every module runs in its own scope — nothing leaks to the global object — and modules are always in strict mode by default, catching mistakes like accidental globals that regular scripts would silently allow.