Organize reusable JavaScript code into clean, modular files with named and default exports.
Learn how variables act as labeled containers for data using const, let, and var to manage lexical scope and reassignment safely in modern applications.
ES6 Modules (import / export) allow you to split JavaScript code into separate, reusable files with strict scope boundaries.
Modules prevent global scope pollution, clarify explicit file dependencies, and enable modern bundlers (Vite, Next.js) to tree-shake unused code.
{ func }.// mathUtils.js (Module File)
export const add = (a, b) => a + b; // Named export
export const subtract = (a, b) => a - b; // Named export
export default function multiply(a, b) { // Default export
return a * b;
}
// app.js (Main File)
import multiply, { add, subtract } from "./mathUtils.js";
console.log(add(2, 3)); // 5
console.log(multiply(2, 3)); // 6
Modules can aggregate utilities into clean entry points:
// index.js
export { add, subtract } from "./mathUtils.js";
import add from './mathUtils.js') attempts to load the default export, resulting in undefined or import errors!./ or ../ relative paths (import { add } from "./mathUtils.js"). Omiting ./ causes Node to look inside node_modules.Use named exports for utility libraries. Next, let's explore JavaScript Proxy Objects!