Large programs usually have their code divided into reusable modules. If you need to reuse more than one function from one file, learning how to export more than one function is essential in JavaScript. This approach helps keep the code clean, modular, and easy to maintain. Instead of repeating the same logic across files, you can define functions once and use them anywhere you need.
Exporting multiple functions enhances collaboration and readability in modern web development, especially with ES6 modules. Whether these are utility functions, helper scripts, or sets of reusable logic across APIs, JavaScript makes it extremely easy to manage and export them efficiently.
Export Multiple Functions in JavaScript Using Named Exports
The most common method for exporting JavaScript function is named exports. You can define multiple functions in single file and export them together. Let’s take an example for basic mathematical operations like addition and multiply. Here’s how it works:
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
export { add, multiply };Now you can import these functions in another file like this:
import { add, multiply } from './math.js';
console.log(add(5, 3));
console.log(multiply(4, 2));This approach keeps each function accessible and easy to import wherever required. It’s perfect for projects where you want to maintain flexibility by choosing specific functions to use.
Using Export Inline for Simplicity
You can also export functions directly when declaring them. This is another way to manage JavaScript module exports more neatly:
export function greet(name) {
return `Hello, ${name}`;
}
export function farewell(name) {
return `Goodbye, ${name}`;
}With this approach, your code becomes more compact. When someone reads your module, they instantly know which functions are available for use.
Export Multiple Functions in JavaScript with Default and Named
Sometimes you may want to export one main function as a default and others as named exports. For example:
export default function calculateTotal(items) {
return items.reduce((sum, item) => sum + item.price, 0);
}
export function formatPrice(price) {
return `$${price.toFixed(2)}`;
}
export function discount(price, percent) {
return price - price * (percent / 100);
}It only fetch selected functions to the execution. Let’s see how you can use above functions to use it into another script.
import calculateTotal, { formatPrice, discount } from './cart.js';
const items = [{ price: 100 }, { price: 200 }];
console.log(formatPrice(calculateTotal(items)));
console.log(discount(100, 10));This type of setup works great for e-commerce projects where you need both a main calculation function and helper utilities in the same module.
Conclusion
Learning how to export multiple functions in JavaScript makes your code modular and scalable. Whether you use named exports, inline exports, or a mix of default and named exports, each approach improves clarity and reduces redundancy. By mastering JavaScript module exports, you can build cleaner, more efficient applications with reusable code that’s easy to manage and share.

