Understanding Named and Default Exports in JavaScript ES6 Modules
In JavaScript, particularly when working with ES6 modules, export and export default are used to share code between different files. Understanding the difference between export const data (a named export) and export default data (a default export) is crucial for organizing and maintaining your code effectively. Here's a comprehensive explanation of why and how to use each:
1. Named Exports (export const data)
What It Is: A named export allows you to export multiple values (variables, functions, classes, etc.) from a module. Each exported item must have a unique name within the module.
// data.js
export const data = { /* ... */ };
export const anotherData = { /* ... */ };
How to Import:
When importing named exports, you must use the exact names of the exported items, and you enclose them in curly braces {}.
// main.js
import { data, anotherData } from './data.js';
Advantages:
Use Cases:
2. Default Exports (export default data)
What It Is: A default export is used to export a single value from a module. A module can have only one default export.
// data.js
const data = { /* ... */ };
export default data;
// Or directly
export default { /* ... */ };
How to Import:
When importing a default export, you can choose any name for the imported value, and you don't use curly braces.
// main.js
import data from './data.js';
Advantages:
Use Cases:
3. Combining Named and Default Exports
A module can have both named exports and a default export. This allows you to export a primary value alongside additional utilities.
// data.js
export const helper = () => { /* ... */ };
const data = { /* ... */ };
export default data;
Importing:
// main.js
import data, { helper } from './data.js';
领英推荐
4. Practical Example
Let's consider a practical scenario to illustrate the differences.
Module with Named Export:
// mathUtils.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
Importing Named Exports:
// app.js
import { add, subtract } from './mathUtils.js';
console.log(add(5, 3)); // Output: 8
console.log(subtract(5, 3)); // Output: 2
Module with Default Export:
// logger.js
const logger = {
log: (message) => console.log(message),
error: (message) => console.error(message),
};
export default logger;
Importing Default Export:
// app.js
import logger from './logger.js';
logger.log('This is a log message'); // Output: This is a log message
logger.error('This is an error message'); // Output: This is an error message
Module with Both:
// config.js
export const API_URL = 'https://api.example.com';
const config = {
appName: 'MyApp',
version: '1.0.0',
};
export default config;
Importing Both:
// app.js
import config, { API_URL } from './config.js';
console.log(config.appName); // Output: MyApp
console.log(API_URL); // Output: https://api.example.com
6. Choosing Between Named and Default Exports
Use Named Exports When:
Use Default Exports When:
Best Practices:
7. Additional Tips
// utilities.js
export { add, subtract } from './mathUtils.js';
export { default as logger } from './logger.js';
import { add as addition } from './mathUtils.js';
export * from './mathUtils.js';