Export
A JavaScript keyword used in ES6 modules to expose functions, objects, or primitive values from a module so they can be used by other programs with import statements.
Overview
The export statement is used when creating JavaScript modules to make specific parts of that module available to other files. There are two types of exports: named exports (can be multiple) and default exports (one per module). Exports are fundamental to modular JavaScript development.
Example
javascript// Named exports (multiple per file) // utils.js export const add = (a, b) => a + b; export const subtract = (a, b) => a - b; export function multiply(a, b) { return a * b; } export class Calculator { // ... } // Import named exports import { add, subtract } from './utils.js'; // Default export (one per file) // Button.js export default function Button({ text }) { return <button>{text}</button>; } // Import default export import Button from './Button.js'; // Mixed exports // api.js export default api Client; export const API_URL = 'https://api.example.com'; export function handleError(error) { // ... } // Import mixed import apiClient, { API_URL, handleError } from './api.js'; // Re-export export { something } from './other-module.js'; export * from './all-exports.js';
Key Points
- Two types: named and default exports
- Named: export const/function/class
- Default: export default value
- One default export per module
- Can combine named and default exports
Learn More
- Import - Import statement
- Module - JavaScript modules
- MDN: export