What is a fallback value?

Currently on : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/create-an-export-fallback-with-export-default/

  1. What is a fallback value?
  2. Why can i use ‘export default’ only once per module?
5 Likes

Hello @pratik1900.
A fallback value is a value you would use when the main thing you requested is not available.
Think about the font-family in CSS. You should always provide a fallback family in case the font you are trying to use is not available for the user.

#my-cool-text{
    font-family: "Roboto", sans-serif;
}

The default export (opposed to the named exports) can be used only once per module. This is because it’s the value imported when the functionality you requested is not found (it’s not one of the named exports of the module).
If there is more than one as default, it wouldn’t be clear which one the program has to import.

37 Likes

Thanks for the reply, but I was wondering about fallback value in the context of Export fallback / export default :confused:

2 Likes

In the case of modules export/import the concept remains the same.

By using an export default, you do create a fallback value.
When you try to import something with a name that hasn’t been specified among the named exports, what you will import is the fallback value given by the export default (if present).

From MDN - export:

Named exports are useful to export several values. During the import, it is mandatory to use the same name of the corresponding object.
But a default export can be imported with any name.

// file test.js
let k; export default k = 12;
// some other file
import m from './test'; 
// note that we have the freedom to use import m instead of import k, because k was default export
console.log(m);        // will log 12
35 Likes

Amazingly well explained, with a “less technical” example, such as CSS: understood it perfectly Simon, thank you very much! :ok_hand::blush:

Damn even better! Hahahaha TY!

Thanks for the explanation. But I thought export default cannot be used with let? (according to the lesson in ES6)

he’s not using let on export default, he used it to declare the variable “k”.

2 Likes

Thanks, @simonebogni . It helped me a lot. Hope you will use your code for good>>>Booe