Course contentsShow
← JavaScript
Lesson 1399 of 2,72331. ModulesFree lesson

Named Exports

Export multiple values from a module by name and understand the named export syntax.

Named Exports

You'll learn how to export multiple values from a module by giving each one a specific name.

The Core Idea

Named exports let you explicitly label and share multiple pieces of code from a single module. Think of it like a toolbox where each tool has its own compartment and label — when someone opens your toolbox, they can see exactly what's available and grab what they need by name.

Unlike exporting a single default item, named exports are perfect when your module provides several distinct utilities, functions, or values that users might want individually.

How Named Exports Work

You use the export keyword with a declaration to create a named export:

// math.js
export const PI = 3.14159;
export function add(a, b) {
  return a + b;
}
export function subtract(a, b) {
  return a - b;
}

Each exported item keeps its exact name. When another module imports from math.js, they'll use these same names to access what they need.

You can also export multiple items in one statement:

// utils.js
const formatDate = (date) => date.toISOString();
const formatCurrency = (amount) => `$${amount.toFixed(2)}`;

export { formatDate, formatCurrency };

This grouped syntax is convenient when you want to clearly see all exports at the bottom of your file.

Why Named Exports Matter

Named exports create a clear contract: "Here's what this module provides, and here's what each piece is called." This makes your code self-documenting and helps prevent naming conflicts, since consumers must explicitly import each named export they want to use.

Key Takeaway: Named exports let you share multiple values from a module with explicit, predictable names, making your module's public API clear and organized.