Why do we use default export?

Edit: The following article is really useful in understanding the same: https://medium.com/@timoxley/named-exports-as-the-default-export-api-670b1b554f65

Tell us what’s happening:
I am unable to understand why default export exists. The help-text in previous section states its use case as : ‘It is also used to create a fallback value for a file or module.’

However, given that the import syntax for default export and named export is different; how is this a fallback to the named export?

Please explain why and how is it useful.

Your code so far


import subtract from './math_functions.js';  
// Only change code above this line

subtract(7,4);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0.

Challenge: Import a Default Export

Link to the challenge:

Why have a default export?

Some people don’t like it. To me, it is a useful distinction some times. For example, if a file is a module or specifically holds a class, or a React component, or a redux reducer, etc. - basically when the file is the thing. When the main purpose of that file is that one thing. You may have other named exports that support that thing, but that thing is what that file is all about. To me, then a default export makes sense. Usually, I can look at the file path/name and know exactly what the default export is going to be.

If the file is a collection of things, then I probably won’t have a default export, just a bunch of named exports. If I have to wonder which thing should be the default export in a file, then probably there shouldn’t be a default export. Or I know what I am importing to so I know if it is the default or a named export of that file.

That’s my interpretation and I think it’s pretty consistent with the rest of the Node world. There is a small, vocal minority that thinks default exports are evil and you shouldn’t use them ever. (I’m still trying to understand their reasons.) But you’re going to see a lot of default exports so you might as well get used to them.

I’m plenty familiar with modules, and the whole text about “fallback values” doesn’t make a lick of sense to me either. Default exports are nice when there is only a single obvious thing to export from a module. React components tend to fall into that category. The rest of the time I tend to avoid them.

Yes, that struck me as odd too. I read that and thought, “Wait, is there some part of default exports I don’t understand? What would that even mean?” I made a mental note to go reread the MDN on it. Thank you for confirming that I’m not crazy.

It’s like it was being summarized by someone who didn’t understand them and just thought that that’s what the word “default” must mean, like it’s used in so many other places in code.

Interestingly, the MDN does essentially say that here. I’m still not sure what they mean by “fallback”. This is going to have to take some more time to think through and research.