What is export default in JavaScript?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In JavaScript modules, export default marks one main value from a file as its default export. It is useful when a module is centered around one primary thing, such as one function, one class, or one configuration object, and you want importing code to reference it without using curly braces.
Core Sections
What export default means
A module can have many named exports, but only one default export. The default export is the module’s “main” exported value.
Another file can import it like this:
Notice that the import uses no curly braces. That is the most obvious syntactic difference between a default export and a named export.
How default export differs from named export
Named exports require the same exported name to be imported with braces.
A default export behaves differently:
The importing file can choose the local name freely. That flexibility is one reason default exports feel convenient, but it can also make large codebases less consistent if different files import the same module under different names.
When default exports are a good fit
Default exports are usually a good fit when the file exists mainly to provide one central thing.
Examples:
- a React component file that exports one component
- a utility file centered on one main function
- a configuration module that exports one object
If the file naturally contains several equally important exports, named exports are often clearer.
You can mix default and named exports
JavaScript allows both styles in the same module.
This works, but it should be used deliberately. Overusing mixed exports can make module interfaces harder to scan.
Re-exporting and dynamic import behavior
When you use dynamic import(), the module object exposes the default export under the .default property.
That detail is important when debugging dynamic imports, because the syntax differs from static import add from ... style.
Design tradeoff: convenience versus consistency
The main tradeoff with default exports is that they are concise but less explicit. Named exports force consistency because the exported name appears in both files. Default exports are smoother for single-purpose modules, but in large teams some developers prefer named exports almost everywhere because they are easier to search and refactor consistently.
Neither approach is universally correct. The question is whether the module really has one clear primary export.
Common Pitfalls
- Assuming
export defaultand named exports use the same import syntax leads to missing or incorrect curly braces. - Using default exports everywhere can make imports inconsistent because each caller can choose a different local name.
- Forgetting that a module can have only one default export causes syntax errors in files that try to declare several of them.
- Mixing default and named exports casually can make the module interface less obvious to readers.
- Forgetting that dynamic imports expose the default export as
.defaultcauses confusion during debugging.
Summary
- '
export defaultmarks one main exported value from a JavaScript module.' - It is imported without curly braces and can be renamed freely by the importer.
- Named exports are better when a module exposes several equally important values.
- Mixing default and named exports is allowed, but it should be done deliberately.
- Choose default export when the module has one clear primary responsibility, not just because the syntax is shorter.
Related reading
- What is HTML5 File.slice method actually doing?
- What is JavaScript's highest integer value that a number can go to without losing precision?
- What is lexical scope?
- What is monkey patching?
- What is not assignable to parameter of type never error in TypeScript?
- What is pip's equivalent of npm install package --save-dev?
- What is the best auto-suggest search algorithm for javascript
- What is the best way to add options to a select from a JavaScript object with jQuery?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.