Named export
Learn to export multiple values with export.
Named export
A named export exports a value by its name. A module can have as many named exports as you like.
Two common forms: export at declaration, or with a separate statement.
export const TAX_RATE = 0.18;
export function formatPrice(p) {
return p + " TRY";
}In another module you can import these values by name.
Multiple exports
A module can hold multiple named exports. Each one is declared separately at the top level of the file.
export const LOW = 10;
export const HIGH = 100;
export function clamp(v) {
return Math.max(LOW, Math.min(HIGH, v));
}You can also gather them in one export statement at the end: export { LOW, HIGH, clamp };.
Exercise Write named exports
In a module, export TAX_RATE and formatPrice as named exports. formatPrice takes a price parameter and returns price + " TRY".
Show example solution
Export one const and one function. The function takes price and returns a string.
Example solution:
export const TAX_RATE = 0.18;
export function formatPrice(price) {
return price + " TRY";
}
Test yourself
-
How many named exports can a module have?
- Only one
- As many as you want
- At most ten
- A limited number
Show answer
As many as you want — Correct! A module can have as many named exports as you want.
-
Export the tax rate
export const TAX_RATE = ____;Show answer
export const TAX_RATE = 0.18; -
Match the export forms
- export const
- export a constant
- export function
- export a function
- named export
- imported by name
- multiple exports
- several values in one file
-
Complete the function name
export function ____(price) { return price.toFixed(2); }Show answer
export function formatPrice(price) { return price.toFixed(2); }