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 widthraw(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 widthraw(amount) {
balance -= amount;
}
function getBalance() {
return balance;
}
export { INTEREST_RATE, deposit, widthraw, getBalance };
You can also give an alias to an exported value with the as
keyword:
export { INTEREST_RATE as interest, deposit, widthraw, 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, widthraw, getBalance };