Saturday, June 29, 2024

JavaScript Default Export vs Named Export in modules

In JavaScript modules, you can export values from a file using either default export or named exports. Each approach serves different purposes and has its own syntax for importing and exporting values.

Default Export:

1. Purpose: Default export is used to export a single value, function, or object from a module. It is commonly used when a module only needs to export one main entity.

2. Syntax:
   // In the exporting file
   export default myFunction;
   export default myValue;
   export default { key: value };

   // In the importing file
   import myDefaultExport from './module';
3. Usage:
   // In the exporting file (e.g., module.js)
   const myValue = 10;
   export default myValue;

   // In the importing file
   import myValue from './module.js';
   console.log(myValue); // Output: 10
Named Export:

1. Purpose: Named exports are used to export multiple values, functions, or objects from a module. They allow you to export several entities from a single module.

2. Syntax:
   // In the exporting file
   export { value1, value2, ... };

   // In the importing file
   import { value1, value2 } from './module';
3. Usage:
   // In the exporting file (e.g., module.js)
   export const value1 = 'foo';
   export const value2 = 'bar';

   // In the importing file
   import { value1, value2 } from './module.js';
   console.log(value1); // Output: 'foo'
   console.log(value2); // Output: 'bar'
Comparison:
  • Default Export: Typically used for exporting a single value or entity from a module. When importing, you can choose any name for the imported value.
  • Named Export: Used for exporting multiple values or entities from a module. When importing, you must use the exact names specified in the export statement.
Both default and named exports are powerful features of JavaScript modules, allowing you to structure and organize your code effectively and facilitate code reuse across your application. Choose the appropriate export method based on the specific requirements of your module.

How can we alias Named Export?
You can alias named exports when importing them in JavaScript. This can be useful if you want to use a different name for an exported value or if you want to avoid naming conflicts.

Here's how you can alias named exports:
// In the exporting file (e.g., module.js)
export const originalName = 'foo';

// In the importing file
import { originalName as aliasName } from './module';
console.log(aliasName); // Output: 'foo'
In this example, we're importing the named export originalName from the module.js file and aliasing it as aliasName. Now, when we refer to aliasName in our code, it will behave exactly the same as if we were referring to originalName.

This aliasing feature is particularly helpful when you have multiple modules with similar or conflicting names, or when you want to provide a more descriptive or meaningful name for an imported value in your code.

No comments:

Post a Comment

Hot Topics