JavaScript modules allow developers to share and reuse code across files efficiently. This is done through export and import statements. JavaScript offers two main ways to export code: Default Exports and Named Exports.
Default exports are used to export a single value or functionality from a module. A module can only have one default export.
- A module can have only one default export.
- Default exports are imported without curly braces
{}
. - The name used to import the default export does not need to match the name of the export.
File: mathUtils.js
export default function multiply(a, b) {
return a * b;
}
File: app.js
import multiplyNumbers from "./mathUtils.js";
console.log(multiplyNumbers(5, 3)); // Output: 15
Explanation:
- In
mathUtils.js
, the functionmultiply
is exported as the default export. - In
app.js
, the default export is imported without curly braces, and the namemultiplyNumbers
is used, even though it was exported asmultiply
.
Named exports allow you to export multiple values or functionalities from a module. A module can have multiple named exports.
- A module can have multiple named exports.
- Named exports must be imported using curly braces
{}
. - The names of the imports must match the names of the exports unless renamed using the
as
keyword.
File: mathUtils.js
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
File: app.js
import { add, subtract } from "./mathUtils.js";
console.log(add(5, 3)); // Output: 8
console.log(subtract(5, 3)); // Output: 2
Explanation:
- The functions
add
andsubtract
are exported as named exports inmathUtils.js
. - In
app.js
, the functions are imported using curly braces, and the names must match the exported names.
You can rename named exports while importing using the as
keyword.
File: app.js
import { add as addition, subtract as subtraction } from "./mathUtils.js";
console.log(addition(5, 3)); // Output: 8
console.log(subtraction(5, 3)); // Output: 2
A module can have both a default export and named exports. This is useful when you want to export a main functionality (default) along with additional utilities (named).
File: mathUtils.js
export default function divide(a, b) {
return a / b;
}
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
File: app.js
import divide, { add, subtract } from "./mathUtils.js";
console.log(divide(10, 2)); // Output: 5
console.log(add(5, 3)); // Output: 8
console.log(subtract(5, 3)); // Output: 2
Explanation:
- The
divide
function is the default export and is imported without curly braces. - The
add
andsubtract
functions are named exports and are imported with curly braces.
Feature | Default Export | Named Export |
---|---|---|
Number of Exports | Only one per module | Multiple per module |
Import Syntax | import value from 'module' |
import { value } from 'module' |
Renaming | Can rename without as |
Requires as for renaming |
Curly Braces | Not used | Required |
Use Case | For the primary functionality | For multiple utilities or values |
Using exports promotes:
- Code Reusability: Functions and variables can be reused across multiple files.
- Separation of Concerns: Each file/module can focus on a specific functionality.
- Maintainability: Modular code is easier to read, debug, and maintain.
- Scalability: Projects become easier to scale as functionalities are modularized.
-
Use Default Exports when:
- The module has one main functionality.
- You want the flexibility to rename the import without using
as
.
-
Use Named Exports when:
- The module contains multiple related utilities or values.
- You need to export multiple functionalities from the same file.
With this understanding, you can choose between default and named exports to structure your JavaScript modules effectively!