JavaScript · Modules · Junior · Best Practice
Explain default vs named exports and re-exports. What pitfalls exist?
Short Interview Answer
Named exports are explicit and tree-shake friendly; default exports are singular sugar that can confuse renames; re-exports forward module public API.
Detailed Explanation
Prefer named exports for APIs with multiple symbols and clearer refactors. Default exports allow any import name, which can hurt consistency and some tooling. `export { x as y } from './mod'` builds barrels — convenient but can accidentally bloat bundles if barrels re-export everything. Circular dependencies behave differently than people expect — design acyclic graphs. Use `import type` in TypeScript to erase types.
Example
export const API_URL = '/api';
export function getUser() {}
export { getUser as fetchUser } from './user.js';Interview Tip
State a team preference (often named exports) with rationale.
Common Mistake
Huge barrel files that defeat code splitting/tree-shaking.