Advertisement
Intermediate JavaScript, ES Modules

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

Short 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.

Save JavaScript mock interview
Advertisement
AModel 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.
Likely follow-ups
  • How does this enable code-splitting in bundlers like Webpack or Vite?
Related topics