Create custom / local modules and export them using various module patterns.

PHOTO EMBED

Wed Apr 23 2025 01:24:58 GMT+0000 (Coordinated Universal Time)

Saved by @signup

🧩 1. ES6 Module Pattern (using export / import)
                          
📁 mathUtils.js
export const add = (a, b) => a + b;
export const multiply = (a, b) => a * b;



📁 main.js
import { add, multiply } from './mathUtils.js';

console.log(add(2, 3));       // 5
console.log(multiply(2, 3));  // 6





🧱 2. CommonJS Module Pattern (Node.js style)

📁 mathUtils.js
function add(a, b) {
    return a + b;
}

function multiply(a, b) {
    return a * b;
}
module.exports = { add, multiply };


📁 main.js
const { add, multiply } = require('./mathUtils');

console.log(add(4, 5));       // 9
console.log(multiply(4, 5));  // 20
content_copyCOPY

https://chatgpt.com/share/6806ee0c-b024-800e-83c3-9fa4fe10af07