Exporting
To export a value, append its declaration with the export
keyword:
// account.js file
let balance = 0;
export const INTEREST_RATE = 0.2;
export function deposit(amount) {
balance += amount;
}
export function withdraw(amount) {
balance -= amount;
}
export function getBalance() {
return balance;
}
You can export values one by one (as it is done above), or you can export all in a single statement at the end of the module:
// account.js file
let balance = 0;
const INTEREST_RATE = 0.2;
function deposit(amount) {
balance += amount;
}
function withdraw(amount) {
balance -= amount;
}
function getBalance() {
return balance;
}
export { INTEREST_RATE, deposit, withdraw, getBalance };
You can also give an alias to an exported value with the as
keyword:
export { INTEREST_RATE as interest, deposit, withdraw, getBalance };
Moreover, you can break a long export statement into several export statements (not a very common practice):
export { INTEREST_RATE as interest };
export { deposit, withdraw, getBalance };