Why would I want to use the export keyword to export a variable as soon as it's declared instead of exporting named exports at the bottom of my file?

Question

Why would I want to use the export keyword to export a variable as soon as it’s declared instead of exporting named exports at the bottom of my file?

Answer

The choice to export a variable as soon as it’s declared or as a named export at the bottom of the file is based on personal preference.

You may find one way of exporting easier to read than another. For example, say you have many variables you need to export - exporting all of the variables at the bottom of the file may make it easier to see the all the variables that are being exported. On the other hand, say you only have a couple variables you need to export, you may want to export the variable when declared as to not clutter your file unnecessarily.

24 Likes

Hello, I am just started to learn JS (forgive me any lack of knowledge). Once I tried to write simple database object in another JS file and then include it as in my HTML. It worked okey. I am just curious, what is the advantage of import-export and diffrenece between exporting objects to another file with import-export feature, and my way of including script to diffrent file in index.html?