| Tutorials | JavaScript |
Introduction to Modules in JavaScript
Modules in JavaScript allow you to split code into smaller, reusable files.
This makes it easier to organize large projects.
Exporting a Module
// file operations.js
export function add(a, b) {
return a + b;
}
Importing a Module
// file main.js
import { add } from './operations.js';
console.log(add(5, 7)); // 12
Default Export
// file greeting.js
export default function greet(name) {
return `Hello, ${name}`;
}
// file app.js
import greet from './greeting.js';
console.log(greet("Carlos"));
With modules, you can keep your code clean and easy to maintain.