Functions and Modules
Functions
JavaScript
ES Modules
Split code across files with import/export (module files need a real Node.js project, so this example is read-only):
// math.js
export function add(a, b) {
return a + b;
}
// app.js
import { add } from './math.js';
console.log(add(2, 3)); // 5
Set "type": "module" in package.json to use ES modules natively.