JavaScript › ES Modules
How do dynamic imports (import()) differ from static import statements?
IntermediateAnswer
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.