How to export a function in JavaScript

Exporting functions in JavaScript modules enables code reusability and organization by making functions available for import in other files. As the creator of CoreUI, a widely used open-source UI library, I’ve exported thousands of utility functions and components across modular JavaScript architectures. From my 25 years of experience in web development, the most effective approach is to use the ES6 export keyword for named exports or export default for single function exports. This pattern provides clean module boundaries and explicit dependency management.

Use the export keyword before function declarations for named exports or export default for single function exports.

// Named exports - multiple functions per module
export function add(a, b) {
  return a + b
}

export function subtract(a, b) {
  return a - b
}

export const multiply = (a, b) => a * b

// Default export - single main function
export default function calculateTotal(items) {
  return items.reduce((sum, item) => sum + item.price, 0)
}

// Alternative export syntax at end of file
function divide(a, b) {
  return a / b
}

function modulo(a, b) {
  return a % b
}

export { divide, modulo }

// Export with different name
export { modulo as remainder }

Named exports use the export keyword before function declarations, allowing multiple exports per module. Default exports use export default for the main function that represents the module’s primary functionality. You can export function declarations, expressions, or arrow functions. The alternative syntax with export { } at the end of the file provides a clear overview of all exports. Use aliases with as to export functions under different names.

This is the same export pattern we use in CoreUI modules to organize utility functions and ensure clean API boundaries across components. Prefer named exports for utility functions and default exports for main component or class exports to maintain consistency and improve code readability.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author