JavaScript10 min read

JavaScript Modules: ES6 Import/Export and Beyond

Master JavaScript modules - ES6 import/export, dynamic imports, and module patterns. Learn how to organize code, share functionality, and build maintainable applications. Essential for modern JavaScript development.

Alex Thompson
December 18, 2025
0.0k0

Modules are how you organize code in modern JavaScript. Instead of one giant file, you split code into logical modules. This makes code reusable, maintainable, and easier to test.

Why Modules?

Modules let you split code into separate files, each with a specific purpose. You can import what you need and export what others need. This is the standard way to build JavaScript applications in 2025.

ES6 Import/Export

ES6 introduced native module syntax. Use export to make functions, classes, or variables available, and import to use them in other files. It's clean, simple, and works everywhere now.

Default vs Named Exports

Learn the difference between default exports (one main thing per file) and named exports (multiple things per file). Understanding when to use each makes your code more organized.

Dynamic Imports

Sometimes you want to load modules only when needed. Dynamic imports let you do this, which is great for code splitting and improving performance. This is how modern apps stay fast.

Best Practices

I'll show you how to structure modules, organize exports, and use dynamic imports effectively. These patterns will make your code professional and maintainable.

#JavaScript#ES6#Modules#Import#Export

Common Questions & Answers

Q1

How do ES6 modules work?

A

ES6 modules use import and export keywords. Export makes functions, classes, or variables available to other files. Import brings them into your file. Modules are automatically in strict mode and have their own scope. Use .js extension and type="module" in script tag.

javascript
// math.js - exporting
export function add(a, b) {
  return a + b;
}

export function subtract(a, b) {
  return a - b;
}

// Default export
export default function multiply(a, b) {
  return a * b;
}

// app.js - importing
import multiply, { add, subtract } from './math.js';

console.log(add(2, 3));        // 5
console.log(subtract(5, 2));   // 3
console.log(multiply(2, 4));   // 8

// Import everything
import * as math from './math.js';
math.add(1, 2);
Q2

What are dynamic imports?

A

Dynamic imports load modules on-demand using import() which returns a Promise. Useful for code splitting, lazy loading, and conditional imports. Improves initial load time by loading code only when needed.

javascript
// Dynamic import - loads module on demand
async function loadModule() {
  const module = await import('./heavy-module.js');
  module.doSomething();
}

// Conditional loading
if (user.isPremium) {
  const premiumFeatures = await import('./premium.js');
  premiumFeatures.enable();
}

// In React - lazy loading components
const LazyComponent = React.lazy(() => import('./LazyComponent'));

// Load multiple modules
const [module1, module2] = await Promise.all([
  import('./module1.js'),
  import('./module2.js')
]);