Understanding export default in JavaScript: Example Codes
What is "export default" in JavaScript?
"export default" is a JavaScript keyword used to export a single entity (e.g., a function, object, or class) from a module for use in other modules. It's a convenient way to designate the primary or default export of a module, especially when you only want to export one item.
How does it work?
- Placement: You place
export default
before the entity you want to export. - Naming: Unlike regular exports, you don't need to specify a name for the exported entity. The
default
keyword implicitly names it. - Import: To import the default export in another module, you simply use the module name without curly braces.
Example:
// module.js
export default function greet(name) {
console.log(`Hello, ${name}!`);
}
// index.js
import greet from './module';
greet('Alice'); // Output: Hello, Alice!
Why use "export default"?
- Simplicity: It's a concise way to export a single entity.
- Clarity: It clearly indicates the primary export of a module.
- Common Practice: It's widely used and understood in the JavaScript community.
In the context of Node.js and ES6:
- Node.js: "export default" is essential for creating reusable modules that can be imported and used in other Node.js applications.
- ES6: This feature was introduced in ES6 (ECMAScript 2015), significantly improving module management and code organization in JavaScript.
Additional Notes:
- You can only have one default export per module.
- If you need to export multiple entities, you can use regular exports (without
default
) and import them using curly braces. - While "export default" is commonly used, it's not strictly necessary. You can also use regular exports if you prefer.
Understanding export default
in JavaScript: Example Codes
Example 1: Exporting a Function
// module.js
export default function greet(name) {
console.log(`Hello, ${name}!`);
}
// index.js
import greet from './module';
greet('Alice'); // Output: Hello, Alice!
module.js
: Here, we define a functiongreet
and export it as the default export usingexport default
.index.js
: We import the default export frommodule.js
usingimport greet from './module';
. Thegreet
function is now available for use inindex.js
.
// person.js
const person = {
name: 'John',
age: 30
};
export default person;
// index.js
import person from './person';
console.log(person.name); // Output: John
person.js
: We create an objectperson
and export it as the default export.index.js
: We import theperson
object and can access its properties.
// car.js
class Car {
constructor(brand) {
this.brand = brand;
}
}
export default Car;
// index.js
import Car from './car';
const myCar = new Car('Toyota');
console.log(myCar.brand); // Output: Toyota
index.js
: We create an instance of theCar
class and can use its methods and properties.
Key Points:
- Single Default Export: A module can have only one default export.
- Import without Curly Braces: When importing the default export, you don't need to use curly braces.
- Flexibility: You can export any type of entity as the default export, including functions, objects, and classes.
- Named exports can also be used in JavaScript, allowing you to export multiple entities with specific names.
- Default exports are often used for the primary or most commonly used entity in a module.
Alternative Methods to export default
in JavaScript
While export default
is a popular and convenient way to export entities in JavaScript, there are other methods that can be used:
Named Exports
- Multiple Exports: You can export multiple entities with specific names.
- Import with Curly Braces: When importing named exports, you need to use curly braces to specify the names of the entities you want to import.
// module.js
export function greet(name) {
console.log(`Hello, ${name}!`);
}
export const PI = 3.14159;
// index.js
import { greet, PI } from './module';
greet('Alice'); // Output: Hello, Alice!
console.log(PI); // Output: 3.14159
Re-exporting
- Exporting from Another Module: You can re-export entities from another module.
- Combining Exports: This is useful for creating a single module that exports entities from multiple other modules.
// module1.js
export function greet(name) {
console.log(`Hello, ${name}!`);
}
// module2.js
export const PI = 3.14159;
// combined.js
export { greet } from './module1';
export { PI } from './module2';
// index.js
import { greet, PI } from './combined';
greet('Alice'); // Output: Hello, Alice!
console.log(PI); // Output: 3.14159
Dynamic Imports
- Lazy Loading: You can import modules dynamically based on certain conditions or user interactions.
- Improved Performance: This can improve performance, especially for large applications.
// index.js
async function loadModule() {
const module = await import('./module');
module.greet('Alice'); // Output: Hello, Alice!
}
loadModule();
Choosing the Right Method:
- Single Default Export: If you have only one primary entity to export,
export default
is a good choice. - Multiple Named Exports: If you need to export multiple entities with specific names, named exports are suitable.
- Re-exporting: Use re-exporting to combine exports from multiple modules into a single module.
- Dynamic Imports: Consider dynamic imports for lazy loading modules, especially in large applications.
javascript node.js ecmascript-6