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

When to Use Named vs Default

Understand the tradeoffs: named exports enable refactoring tools, defaults allow flexible naming.

When to Use Named vs Default

You'll learn how to decide between named and default exports based on refactoring needs and naming flexibility.

The Core Trade-off

Both named and default exports get the job done, but they optimize for different priorities:

Named exports force explicit naming at the source. When you export { calculateTax, formatCurrency }, every file that imports these must use those exact names (unless renamed with as). This consistency is tooling gold—your IDE can safely rename calculateTax across your entire codebase with one refactor command.

Default exports give importers freedom. Since there's no prescribed name, someone can write import MyThing from './thing.js' or import Thing from './thing.js' or whatever makes sense in their context. This flexibility is convenient for the importer but makes automated refactoring fragile—tools can't reliably track what "the default export from X" is called everywhere.

When to Choose What

Prefer named exports when:

  • You're exporting multiple related items (a utility library with several functions)
  • You want refactoring tools to track usage reliably
  • Consistent naming across files matters to your team

Use default exports when:

  • Your module has one clear "main thing" (like a React component or a class)
  • Importers should decide the name based on their context
  • The export is conceptual rather than literally named (like a configuration object)

Think of named exports like branded products on a shelf—everyone calls Kleenex "Kleenex." Default exports are like generic items—you call them whatever fits your needs.

Key Takeaway: Named exports enable better tooling and consistency; default exports offer flexibility. Choose based on whether refactoring safety or importer convenience matters more for that module.