In JavaScript, modules refers to independent and reusable code. Using modules in JavaScript, you can write code in one file and then share the same in another file so as to achieve code maintainability and reusability. Import and export keywords help you refer to modules and other code snippets written in another file.
As the names suggest, the export keyword is used to expose some piece of code written in a file and the import keyword is used to fetch some piece of code exported from another file. Now, there are two types of exports and imports - default export-import and named export-import.
The default export is written with the default keyword and there can be only one default export in a file. This means that only one code snippet can be exported as default from a file and thus, be imported in another file using default import. The default import should have the same name which is exported. Let’s take the help of an example to see how you write it. Suppose that you have a file named ‘utils.js’ where you have written a function for finding the sum of given numbers.
utils.js
const sum = (...arr) => { return arr.reduce((accumulator, currentValue) => accumulator + currentValue); } export default sum;
Alternatively, you can also write like the one given below where you can directly export the anonymous function sum by writing the export default keywords along with the function declaration:
utils.js
export default sum = (...arr) => { return arr.reduce((accumulator, currentValue) => accumulator + currentValue); }
Now, in another file named ‘script.js’, you can access this function as:
script.js
import sum from 'utils.js'; console.log(sum(1, 2, 3, 4));
A named export can be used to expose more than code snippets written in a file and thus, all of them can be imported in some other file using named import. Note that the names of the code snippets to be exported-imported using named export-import are written within curly braces. This is how the default export is distinguished from named export. In default export, the default keyword is used whereas in named export, the names are written inside curly braces {}.
Consider that you have one more function which is used to calculate average of given numbers in the file named ‘utils.js’ and you want to export both the functions - sum as well as average.
utils.js
const sum = (...arr) => { return arr.reduce((accumulator, currentValue) => accumulator + currentValue); } const average = (...arr) => { return sum(...arr) / arr.length; } export {sum, average};
Now, in another file named ‘script.js’, you can access the sum function and average function individually as:
script.js
import {sum} from 'utils.js'; import {average} from 'utils.js'; console.log(sum(1, 2, 3, 4)); console.log(average(1, 2, 3, 4));
You can combine both the imports and thus write them in the same line as:
script.js
import {sum, average} from 'utils.js'; console.log(sum(1, 2, 3, 4)); console.log(average(1, 2, 3, 4));
You can optionally choose to give an alternative name to the named export using the ‘as’ keyword. Thus, you can write:
script.js
import {sum as add} from 'utils.js';
And then, you can access the sum function by the identifier add.
What if there are a lot of exported functions? Will you write all of them separating each other using comma? Well, you have got simpler method to reference all the named exports at once. You can use * followed by keyword ‘as’ followed by the alias name that you want to give to all the named exports. Thus, you can write:
script.js
import * as fn from 'utils.js'; console.log(fn.sum(1, 2, 3, 4)); console.log(fn.average(1, 2, 3, 4));
Here, ‘fn’ is the alias name given to all the functions exported from the ‘utils.js’ file using named exports.