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

Default Export Syntax

Discover how to export a single main value from a module using export default and when to use it.

Default Export Syntax

What you'll learn: How to export a single main value from a module using export default and when it's the right choice.

The One Thing Per Module Pattern

Sometimes a module has one clear "star of the show"—a single main function, class, or value that represents its primary purpose. For these cases, JavaScript provides default exports.

Think of it like a restaurant's signature dish. Yes, they might have other menu items (named exports), but there's one standout specialty that defines the place. That's your default export.

How Default Export Works

Use the export default keyword followed by the value you want to export as the module's main offering:

// utils.js
export default function greet(name) {
  return `Hello, ${name}!`;
}

Or with a class:

// User.js
export default class User {
  constructor(name) {
    this.name = name;
  }
}

You can also export any expression:

// config.js
export default {
  apiUrl: 'https://api.example.com',
  timeout: 5000
};

Key Differences from Named Exports

  • One per module: You can only have one export default per file
  • No curly braces when importing: Makes importing cleaner
  • Any name when importing: The importer chooses the name

When to Use Default Exports

Use export default when:

  • Your module has one clear primary purpose
  • You're exporting a single function or class
  • You want simpler import syntax for the main export

Avoid when you have multiple equally-important exports—stick with named exports instead.

Key Takeaway: Use export default to designate one main export per module, making it clear what the module's primary purpose is and simplifying imports.