Thursday, July 2, 2026

Javascript module

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.

  • 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, pi and add() can be used in other files.

import:

Use import to bring exported things into another file.

app.js

import { pi, add } from "./math.js";

console.log(pi); // 3.14
console.log(add(2, 3)); // 5

Default 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();

No comments:

Post a Comment

Javascript module

JavaScript Modules (ES6) let you split code into multiple files and reuse code cleanly. Main keywords: export → make something available...