One of the good features introduced in ES6 was javascript modules in an efficient way by which we can export
and import
variables, functions, and classes between different .js
files.
We have two different ways to export: Named exports and Default exports. To properly understand the default export, we must first understand the named export well.
Named export
In this case, in the source
file, we export the desired variables, functions, or classes that have a specific name. The syntax is as follows:
// file: source.js
export const myVariable = /* … */
export function myFunction() { /* … */ }
export class myClass { /* … */ }
Now, to access the above items in the target
file, we must import them as follows:
// file: target.js (in the same directory as the source.js file)
import { myVariable } from "./source.js"
import { myFunction } from "./source.js"
import { myClass } from "./source.js"
Now it's time to get to the main question "what exactly is the default export"?
Default export
Except for the cases where we exported them by name (named exports), there is a similar feature called default export that can be used only once in each .js
file. See the following example and compare it with the previous source.js
file:
// file: source.js
export default function myNewFunction() { /* … */ }
export const myVariable = /* … */
export function myFunction() { /* … */ }
export class myClass { /* … */ }
In fact, each .js
file can have "multiple named exports" and "only one default export"_ here myNewFunction
is exported as default
. With this, when importing in the target
file, javascript understands which item is exported as default
.
The item that is "exported as default" (myNewFunction
) is imported in the target.js
file as follows:
// file: target.js (in the same directory as the source.js file)
import anyName from "./source.js"
Look carefully at the differences! Here, we don't have { }
sign after import
, and we used a custom name that we didn't have in the source
file. Here anyName
represents myNewFunction
.
This shows that we can give "any desired name" to the item that is "exported as default" when importing it and just pointing to the "path" of the source
file, JavaScript will find that file and import it.
Some important notes:
Unlike named exports, in default export we don't need to export named
items and we can export "unnamed" items as well.
Why did they add the Default export feature to ES6 at all!? for the ability
to export "unnamed items" (anonymous functions and classes) as well as
expressions and even object literals in addition to named items.
export
keyword details here. Currently it is not supported natively by any of the web browsers. – Nonalignment