JavaScript import()

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

import(path) returns a Promise that resolves to a module namespace object. Use it for code-splitting and conditional loading.

JavaScript Syntax

JavaScript
Promise.resolve({ default: 1, hello: () => "hi" })
  .then((mod) => {
    console.log(mod.hello());
  });
Try it Editor

How It Works

  1. Simulated module. Real import('./x.js') returns a similar namespace.
  2. Async. Use await inside async functions.

Examples

Example 1 — await import()

Inside async functions.

JavaScript
async function load() {
  const mod = await Promise.resolve({ value: 7 });
  console.log(mod.value);
}
load();
Try it Editor

How It Works

  1. await waits for the module.

Example 2 — Conditional load

Import only when needed.

JavaScript
const need = true;
if (need) console.log("import('./heavy.js')");
Try it Editor

How It Works

  1. Keeps startup smaller.

Example 3 — Path must be reachable

Bundlers and browsers resolve differently.

JavaScript
console.log("use relative or absolute URLs");
Try it Editor

How It Works

  1. Follow your runtime rules.

Frequently Asked Questions

No. Static import is declarative at the top level. import() is a function call that returns a Promise.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful