Default Exports
Default Exports
Section titled “Default Exports”Introduction
Section titled “Introduction”A default export is the main export of a module. Each module can have at most one default export. When importing, you can name it anything.
Exporting Default
Section titled “Exporting Default”// 📁 User.jsexport default class User { constructor(name) { this.name = name; }}
// 📁 format.jsexport default function formatDate(date) { return date.toISOString();}
// 📁 config.jsexport default { theme: 'dark', lang: 'en'};Importing Default
Section titled “Importing Default”// You can name the import anything you wantimport User from './User.js';import format from './format.js';import settings from './config.js';
console.log(settings.theme); // 'dark'Combining Named and Default
Section titled “Combining Named and Default”// 📁 user.jsexport default class User { constructor(name) { this.name = name; }}export function formatName(user) { return user.name.toUpperCase();}
// 📁 main.jsimport User, { formatName } from './user.js';
const user = new User('Alice');console.log(formatName(user)); // 'ALICE'Default vs Named
Section titled “Default vs Named”| Feature | Default Export | Named Export |
|---|---|---|
| Per module | 1 | Unlimited |
| Import name | Any name | Must match export |
| Syntax | export default | export const / export function |
| Re-export | export { default as X } | export { X } |
Summary
Section titled “Summary”- Use default exports for the main exported value
- Import name can be anything
- Combine with named exports when needed
- Good for: classes, main functions, single object configs