Default Export/Import

Suppose you want to export a single value (or when one value is the primary element in a module). In that case, you could use a default export:

// account.js file
const INTEREST_RATE = 0.2;

class Account {
  constructor(balance) {
    this.balance = balance;
  }

  deposit(amount) {
    this.balance += amount;
  }

  widthraw(amount) {
    this.balance -= amount;
  }
}

export default Account;
export { INTEREST_RATE as interest };

When importing a "default export," you do not use curly brackets.

// script.js file
import Account from "./account.js";

const checking = new Account(100);
checking.deposit(20);

console.log(checking.balance);

It is possible to import both default and non-default exports in one import statement. The syntax is as follows:

// script.js file
import Account, { interest } from "./account.js";

console.log(interest);

const checking = new Account(100);
checking.deposit(20);

console.log(checking.balance);