Every Thing You Should Know About JavaScript Export

Until now we have learned about the differences between named export and default export and how to use them.

And there are some important usage cases to understand.

⚫ Using named and default export at the same time.

We are able to use the named and default export at the same time, it means using those to exports in the same file.

Here we have set the func as the default export and arr and obj as named exports, Only able to use a single default export and can use multiple named export

export { func as default, arr, obj };

When importing, We are able to use any name to func and use curly braces to import named export.

import anyName, { arr, obj } from "./index.js";

⚫ Exports are live, values mutate according to changes.

We are able to mutate values in the import file, and they do change. Be more careful in this case.

//index.js
let num = 1;
export function adding() {
return num++;
}
//test.js
console.log(add()); // 1
console.log(add()); // 2
console.log(add()); // 3

⚫ Something about imports.

  1. Using different name when importing.
import {reallyReallyLongModuleExportName as shortName} from 'file';

2. Importing the entire module

import * as name from “module-name”;
// usage
name.Func();
name.array();
name.object();

⚫ Where devs confuse.

There are two types of export in Node.js, Don’t use them unless you use node.

exports.moduleName 
module.exports = { }