| Nullish coalescing (??) | Connecting JS with REST APIs | |
📦 JavaScript Modules |
A JavaScript module is a file that encapsulates related code — variables, functions, classes — and exports them for use in other files. Modules help organize code, improve reusability, and support maintainability in large applications.
JavaScript modules are self-contained, reusable blocks of code that help organize and structure large applications. Instead of writing a single, long script, you can divide code into separate files based on their functionality. This approach encapsulates logic, prevents naming conflicts, and improves code readability and maintainability.
The standard module format in modern JavaScript is ES Modules, which uses import and export statements. ES modules are the recommended standard for both client-side and server-side development.
Export a module: Use the export keyword before a function, variable, or class to make it available to other modules.
Import a module: Use the import statement to bring exported functionality from another file.
You can have multiple named exports from a single module.
utils.js
//javascript
export const pi = 3.14;
export function add(a, b) {
return a + b;
}
main.js
//javascript
import { add, pi } from './utils.js';
console.log(add(5, 3)); // Output: 8
console.log(pi); // Output: 3.14
// file: math.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
// or default export
export default function multiply(a, b) {
return a * b;
}
// file: app.js
import { add, subtract } from './math.js';
import multiply from './math.js';
console.log(add(2, 3)); // 5
console.log(multiply(4, 5)); // 20
// file: math.js
const add = (a, b) => a + b;
module.exports = { add };
// file: app.js
const { add } = require('./math');
console.log(add(2, 3)); // 5
You can have only one default export per module. When importing, you can give it any name you like.
math.js
//javascript
export default function multiply(a, b) {
return a * b;
}
main.js
//javascript import myMultiplyFunction from './math.js'; console.log(myMultiplyFunction(4, 5)); // Output: 20
To enable module functionality, include the type="module" attribute in your <script> tag.
html <script type="module" src="main.js"></script>
Node.js supports ES modules natively. To use them, you must either:
Introduced to load modules asynchronously and on-demand, dynamic imports use the import() function syntax, which returns a promise.
Syntax
//javascript
const myModule = await import('./myModule.js');
myModule.myFunction();
Before ES modules were standardized, CommonJS was the standard for module management in Node.js.
Exports: Uses module.exports to export values.
Imports: Uses require() to import modules.
Loading: Modules are loaded synchronously, which can block execution.
utils.js (CommonJS)
//javascript
function add(a, b) {
return a + b;
}
module.exports = add;
main.js (CommonJS)
//javascript
const add = require('./utils.js');
console.log(add(5, 3)); // Output: 8
type="module" in HTML to enable ESM:
<script type="module" src="app.js"></script>
import() for dynamic imports:
import('./math.js').then(module => {
console.log(module.add(2, 3));
});
| Nullish coalescing (??) | Connecting JS with REST APIs | |