🧩 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
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter