Home >Common Problem >The difference between export and export default
The difference between export and export default is that the export keyword is used to export one or more variables, functions or classes, while the export default keyword is used to export a default variable, function or class. In other modules, you can use the import keyword to import these exported variables, functions, or classes.
export and export default are keywords used to export modules in ES6, and they have some differences.
First of all, the export keyword is used to export one or more variables, functions or classes. You can use the export keyword to export one or more variables, functions, or classes so that other modules can use them. For example, we can export a function as follows:
export function add(a, b) { return a + b; }
In other modules, we can use the import keyword to import the function:
import { add } from './math'; console.log(add(2, 3)); // 输出 5
We can also use the export keyword to export multiple Variable, function or class. For example:
export const PI = 3.14159; export function multiply(a, b) { return a * b; } export class Circle { constructor(radius) { this.radius = radius; } getArea() { return Math.PI * this.radius * this.radius; } }
In other modules, we can use the import keyword to import these variables, functions or classes:
import { PI, multiply, Circle } from './math'; console.log(PI); // 输出 3.14159 console.log(multiply(2, 3)); // 输出 6 const circle = new Circle(5); console.log(circle.getArea()); // 输出 78.53975
export default keyword is used to export a default variable, function or class kind. There can only be one default export per module. For example, we can use a function as the default export:
export default function subtract(a, b) { return a - b; }
In other modules, we can use the import keyword to import the default export:
import subtract from './math'; console.log(subtract(5, 2)); // 输出 3
It should be noted that when importing the default export You can give it any name without using curly braces. This is because the default export is unique, so imported variable names can be used directly.
To summarize, the export keyword is used to export one or more variables, functions or classes, while the export default keyword is used to export a default variable, function or class. In other modules, you can use the import keyword to import these exported variables, functions, or classes.
The above is the detailed content of The difference between export and export default. For more information, please follow other related articles on the PHP Chinese website!