ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

JavaScript Tutorial• 6 min read

ES Modules: import, export & Tooling

Named vs default exports, dynamic import(), npm packages, and modern bundlers

ES Modules (ESM) are the official ECMA standard for modular JavaScript code organization, featuring static analysis, tree-shaking, and asynchronous dynamic loading.

Named vs Default Exports

Named Exports (export const add = ...): Export multiple functions or values per module. Imported with curly braces: import { add } from "./math.js";.

Default Export (export default App): Exactly one default export per file. Imported without braces: import App from "./App.js";.

Dynamic import("./module.js"): Returns a Promise resolving to the module, enabling route-based code splitting and on-demand lazy loading in web apps.

Dynamic Lazy Loading with import()
// Button click triggers dynamic code chunk download
document.getElementById("exportBtn").addEventListener("click", async () => {
  // Only downloads pdfGenerator.js when user actually clicks the button!
  const { generatePDF } = await import("./utils/pdfGenerator.js");
  generatePDF({ title: "Codexia Report", timestamp: Date.now() });
});
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which syntax enables code-splitting and on-demand lazy loading of JavaScript modules?