Course contentsShow
← JavaScript
Lesson 1420 of 2,72331. ModulesFree lesson

What is Dynamic Import?

Understanding dynamic import as a function-like syntax that loads modules at runtime rather than statically.

What is Dynamic Import?

You'll learn: What dynamic import is and how it differs from the static module imports you've been using so far.

The Core Idea

Until now, every import statement you've written has been static — it sits at the top of your file and loads modules before any of your code runs. Dynamic import is different: it's a function-like syntax (import()) that loads modules on demand, during runtime, whenever and wherever you call it.

Think of static imports like ordering all your groceries at the start of the week — everything arrives before you begin cooking. Dynamic imports are like ordering food delivery exactly when you're hungry — you fetch what you need, when you need it.

How It Works

Dynamic import uses import() as if it were a function (though technically it's special syntax). It returns a Promise that resolves to the module object:

// Static import (what you've been doing)
import { calculateTax } from './tax.js';

// Dynamic import (new!)
import('./tax.js')
  .then(module => {
    const result = module.calculateTax(100);
    console.log(result);
  });

You can also use await with it:

async function processOrder() {
  const taxModule = await import('./tax.js');
  const result = taxModule.calculateTax(100);
}

Why It Matters

Dynamic imports let you:

  • Load code only when certain features are activated
  • Split large applications into smaller chunks that load on demand
  • Conditionally import modules based on runtime conditions

Key Takeaway: Dynamic import uses import() syntax to load modules as Promises at runtime, unlike static imports that load everything upfront before code execution begins.