export default vs export
date
Nov 5, 2024
slug
export-default-vs-export
status
Published
tags
JavaScript
Syntax
summary
type
Post
There are two different ways to export code from a file, making code available for use in other modules:
named export
and default export
. You can have multiple named exports per module but only one default export.export default
Sample code
export
After the
export
keyword, you can use let
, const
, and var
declarations, as well as function or class declarations. You can also use the export { name1, name2 }
syntax to export a list of names declared elsewhere. Note that export {}
does not export an empty object — it's a no-op declaration that exports nothing (an empty name list).You can also rename named exports to avoid naming conflicts:
Sample code
Differences
When importing this module, named exports must be referred to by the exact same name (optionally renaming it with
as
), but the default export can be imported with any name.