Modules (Import/Export) in JavaScript
Modules in JavaScript allow you to organize code into separate files and share code across different parts of your application. Using the import
and export
keywords, JavaScript modules make it easy to write, maintain, and test code by encapsulating functionality. Modules were introduced in ES6 (ECMAScript 2015) and are now a standard way to manage JavaScript code in modern applications.
1. Exporting in JavaScript
JavaScript provides two main types of exports: named exports and default exports. Both allow you to make functions, variables, or objects available to other files.
1.1 Named Exports
Named exports allow you to export multiple items from a single module by their names. Each exported item must have a unique name.
Example: Using named exports
// math.js
export const PI = 3.14159;
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
In this example, PI
, add
, and subtract
are exported from math.js
using named exports. Each of these items can be imported individually by other modules.
1.2 Default Exports
Default exports allow you to export a single item as the default from a module. Unlike named exports, default exports do not need a specific name when imported.
Example: Using a default export
// greeting.js
export default function greet(name) {
return `Hello, ${name}!`;
}
Here, greet
is the default export from greeting.js
. Only one default export is allowed per module, and it can be imported without curly braces.
2. Importing in JavaScript
The import
statement allows you to bring in functionality from other modules. You can import named exports, default exports, or both from a single module.
2.1 Importing Named Exports
When importing named exports, you need to match the names exactly as they were exported and enclose them in curly braces.
Example: Importing named exports
// main.js
import { PI, add, subtract } from './math.js';
console.log(PI); // Outputs: 3.14159
console.log(add(2, 3)); // Outputs: 5
console.log(subtract(5, 2)); // Outputs: 3
In this example, PI
, add
, and subtract
are imported from math.js
using their exact names.
2.2 Importing a Default Export
Default exports are imported without curly braces. You can also rename the import to any name of your choice.
Example: Importing a default export
// app.js
import greet from './greeting.js';
console.log(greet("Alice")); // Outputs: Hello, Alice!
Here, greet
is imported from greeting.js
without curly braces, since it is a default export. You can use any name to refer to this imported function.
2.3 Importing Both Named and Default Exports
You can import both named and default exports from the same module.
Example: Importing both named and default exports
// utilities.js
export const version = "1.0";
export default function info() {
return "Utility module";
}
Example: Importing both in another file
// main.js
import info, { version } from './utilities.js';
console.log(info()); // Outputs: Utility module
console.log(version); // Outputs: 1.0
In this example, info
is the default export, and version
is a named export from utilities.js
. Both are imported together in main.js
.
3. Renaming Imports and Exports
JavaScript modules allow you to rename variables when importing or exporting them, which can be useful to avoid naming conflicts.
Example: Renaming Exports
You can rename exports by specifying the new name using the as
keyword.
// library.js
export function calculate() { /*...*/ }
export { calculate as compute };
Example: Renaming Imports
You can also rename imports using as
.
// app.js
import { compute as calculate } from './library.js';
calculate();
In this example, compute
is renamed to calculate
when imported.
4. Importing All Exports
You can import all named exports from a module as an object, which is useful when a module exports multiple items.
Example: Importing all exports
// constants.js
export const MAX_USERS = 100;
export const MIN_USERS = 1;
export const APP_NAME = "MyApp";
Example: Importing all in another file
// config.js
import * as constants from './constants.js';
console.log(constants.MAX_USERS); // Outputs: 100
console.log(constants.MIN_USERS); // Outputs: 1
console.log(constants.APP_NAME); // Outputs: MyApp
In this example, all named exports from constants.js
are imported into the constants
object in config.js
.
Conclusion
Modules in JavaScript allow you to organize code by dividing it into separate files and exporting specific parts for use in other files. The import
and export
keywords make it easy to share functionality between different parts of an application. Using named and default exports, as well as import techniques like renaming and importing all, JavaScript modules improve code structure, readability, and maintainability in larger applications.