Back to Lessons

JavaScript Modules ES6

April 5, 2026

ES6 Modules

Organize code into reusable modules with import/export.

Module Example

// math.js
export const PI = 3.14159;
export function add(a, b) {
    return a + b;
}
export default function multiply(a, b) {
    return a * b;
}

// main.js
import multiply, {PI, add} from "./math.js";
console.log(multiply(2, 3)); // 6

Key Points

  • export default one per module.
  • export/import named exports.
  • Static analysis, tree-shaking friendly.
  • Type="module" in script tag or .mjs files.