Home > Common Problem > The difference between export and export default

The difference between export and export default

百草
Release: 2023-10-12 10:24:11
Original
1940 people have browsed it

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.

The difference between export and export default

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;
}
Copy after login

In other modules, we can use the import keyword to import the function:

import { add } from './math';
console.log(add(2, 3)); // 输出 5
Copy after login

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;
  }
}
Copy after login

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
Copy after login

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;
}
Copy after login

In other modules, we can use the import keyword to import the default export:

import subtract from './math';
console.log(subtract(5, 2)); // 输出 3
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template