JavaScript Modules (ES6) let you split code into multiple files and reuse code cleanly.
Main keywords:
export → make something available from a file to outside.
export → make something available from a file to outside.import → use something from another file into a file.
import → use something from another file into a file.export:
Use export when you want to share variables, functions, or classes from a file.
math.js
export const pi = 3.14;
export function add(a, b) {
return a + b;
}
Here,piandadd()can be used in other files.
import:Use
importto bring exported things into another file.app.js
import { pi, add } from "./math.js";
console.log(pi); // 3.14
console.log(add(2, 3)); // 5Default Export
A file can have one default export.
user.js
export default function greet() {
console.log("Hello");
}Import it without
{}app.js
import greet from "./user.js";
greet();
.jpeg)