Default exports or named exports: Why not both

instagram | bugendaitech  Twitter | bugendaitech  Linkedin | bugendaitech

First, let’s understand what is export, it’s a statement that is used to create and export the function, object, from the given file or module. Then the exported module can be used in the other files with help of import statements. It provides the facility to import only the necessary pieces of code from the other module. While working on a project in budendaitech I got a chance to understand it in brief. 

There is two ways of export files – 

  1. Named Export
  2. Default Export 

1) Named Export

Using named export statements can export multiple functions, primitives, or objects from a single module, then import the exported files in the required module. Also, It is a must to use an exported name when you import the file.

Named Export

For example, the ‘str’ module exported as a named export. In the 2nd image importing the modules, it is a must use curly braces and using export name while importing. 

Also can import multiple objects or functions from the module as shown in the given example below.

Import multiple objects or functions

2) Default Export

 Default export, using this can export a single module from the file, an object, or anything else. In the case of default, the export can import files by any name.

 In the given example below, It is shown that we have exported the demo function as a default export. Now, in the 2nd image module, we are importing it as a ‘demo’ module from the file as default export and then we export the file by importing in the other file. Now in the 3rd image, we are importing the same function as ‘abc’ which shows that we can import any name.

Named Exports vs Default Exports 

If you are working with React.js, ECMAScript, Javascript there is a maximum time needed to export a file to get that file and import to use it. As we know a named export file is useful for exporting multiple files for the files, so If you need to use multiple functions, objects or primitives from a single module then this can be 

implemented by using a named export. On the other hand, if the requirement is to export a single file, so can export it as a default export and import by any name.

Default exports vs named exports


In this article, there is an explanation of export, import, and comparisons between them in brief. Both ways are good and can be used in different -2 scenarios. For more knowledge can visit given link clickhere

Leave a reply