Dynamic import() in JavaScript
import() loads modules on demand, returning a promise of the namespace — route-based code splitting (load the admin panel only when visited), conditional loading (heavy library only when needed), and computed specifiers (locale files by variable) that static imports cannot express.
Top-level static imports hoist and preload; dynamic imports defer to execution time with full error handling (try/catch around await import). Bundlers split at every import() automatically — architecture expressed as syntax.
Load on demand
const m = await import("./heavy.js") — module fetches and evaluates exactly when reached.
Computed specifiers
await import(`./locales/${lang}.js`) — variable paths impossible statically, routine dynamically.
Handle failures
Network/module errors reject — try/catch fallbacks keep features optional, never fatal.
Bundlers split here
Every import() becomes a separate chunk loaded on demand — route splitting without configuration.
async function loadMath(op) { if (op === "fancy") { let m = await import("./fancy-math.js"); return m.compute(2); } return 4;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.