Hacker News new | ask | show | jobs
by ljharb 3134 days ago
A default export is what a module is; a named export is what a module has.

Both tools are useful; both are necessary; most modules should have a single default export.

2 comments

> Both tools are useful; both are necessary

Maybe a nitpick, but given that most other programming languages don't distinguish named vs default, it's probably not necessary. You can do all your programming where every module is something (like in Java) or where every module only has things (like in Python), but there's certainly value in being able to express one or the other in the same language. The downside is that the named/default distinction adds language complexity and learning curve, makes tooling support a little harder, and makes things like CommonJS interop more of a pain.

"Maybe a nitpick, but given that most other programming languages don't distinguish named vs default, it's probably not necessary"

Not sure that how other languages do things really plays a part in how JS does things?

I think his point is that having them available is necessary, because they both have strong use cases.

That's a good way of putting it. I often have modules that ARE one thing. By using the module, you're using the entirety of that one thing. For example, a JSX component.