JavaScript › ES Modules

How do dynamic imports (import()) differ from static import statements?

Intermediate

Answer

import() is a function-like expression that returns a Promise resolving to the module, and can be called anywhere at runtime — including conditionally — unlike static import which must appear at the top level and is resolved before any code runs.
button.addEventListener('click', async () => {
  const { openModal } = await import('./modal.js');
  openModal();
});
Real-world example Lazy-loading a heavy chart library only when the user navigates to a page that actually needs it.

Common follow-ups: How does this enable code-splitting in bundlers like Webpack or Vite?