Mastering JavaScript Export Syntax: A Practical Guide for Developers
Everything You Need to Know About export, export default, and Module Aggregation
If you're building modern JavaScript applications—whether for the browser or Node.js—ES Modules (ESM) are now the standard. And to make your codebase clean, maintainable, and modular, understanding how to use the export
keyword is absolutely essential.
This article breaks down everything you need to know about JavaScript export
syntax. It's not just a list of possibilities—it also gives you the why and when behind each pattern.
🔹 Why Use export
?
In ES Modules, the export
keyword allows you to share values (variables, functions, classes, constants) from one file to another. Without it, each .js
file is isolated. With it, you can build powerful modular architectures where each file only contains what it needs to.
📦 Types of Exports in JavaScript
There are two primary types of exports:
- Named exports – you export one or more identifiers explicitly.
- Default exports – you export one primary thing per file.
✅ Named Exports: Precise and Flexible
Use named exports when you want to export multiple values from a file.
// Exporting declarations directly
export const PI = 3.14;
export function add(a, b) { return a + b; }
export class Circle { constructor(r) { this.r = r; } }
You can also export already-declared variables using an export list:
const min = 1;
const max = 100;
export { min, max };
Need to rename exports? Use the as
keyword:
export { min as minimum, max as maximum };
You can even export destructured variables:
const config = { port: 8080, env: 'production' };
export const { port, env: environment } = config;
✅ Default Exports: One Main Thing
Use a default export when your module is focused on one primary value.
// Inline default export
export default function greet(name) {
return `Hello, ${name}!`;
}
You can also default-export classes or expressions:
export default class User { /* ... */ }
export default [1, 2, 3]; // valid!
export default { version: '1.0' }; // also valid
Or even anonymously:
export default function () { /* unnamed function */ }
export default class { /* unnamed class */ }
🔍 Important: A file can only have one default export. Named exports, on the other hand, can be unlimited.
🔄 Mixing Default and Named Exports (Careful!)
You can mix both in one file:
export default function handler() { /* ... */ }
export const config = { runtime: 'edge' };
However, be cautious—it can lead to confusing imports on the consumer side, especially for new developers.
🔁 Re-exporting and Aggregating Modules
You can re-export values from other modules without importing them:
// Re-export everything
export * from './utils.js';
// Re-export with namespace
export * as utils from './utils.js';
// Re-export selected items
export { validate, format } from './helpers.js';
// Re-export with aliases
export { default as Logger } from './logger.js';
This is especially useful for building public APIs from deeply nested modules.
⚠️ Common Pitfalls and Tips
- Avoid multiple default exports – it's a syntax error.
- Be consistent – decide when to use named vs default and stick to it.
- Auto-import tools in editors like VSCode often default to
default import
, even if you're using named exports. - Prefer named exports for utility libraries (e.g.
utils.js
), as they are easier to tree-shake and scale. - Default exports are great for React components, or a file that exposes a single class/function.
📌 Real-World Examples
React Example (default + named):
// MyComponent.js
export default function MyComponent() { /* ... */ }
export const helper = () => { /* ... */ };
// App.js
import MyComponent, { helper } from './MyComponent';
Utility Module (only named exports):
// math.js
export function add(a, b) { return a + b; }
export function subtract(a, b) { return a - b; }
// index.js
import * as math from './math';
math.add(1, 2);
🧠 Finally
Understanding export
is more than just memorizing syntax. It's about designing a module system that makes your codebase scalable and intuitive.
Use default exports for primary concepts, and named exports for everything else. Don’t be afraid to group and re-export modules to keep your import paths clean.
✅ Good exports make good codebases.
Comments ()