The 'export' Keyword
You'll learn how to make values available for other modules to import using the export keyword.
What Export Does
In ES Modules, each file has its own module scope — variables and functions defined in one file are private by default and invisible to other files. The export keyword is how you explicitly share values, making them available for other modules to import and use.
Think of a module like a workshop with its own tools and materials. By default, everything stays inside. The export keyword is like placing specific items on a public shelf where other workshops can come and borrow them.
Basic Export Syntax
You can export variables, functions, classes, or any value:
// math.js
export const PI = 3.14159;
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
You can also declare first, then export later:
// utils.js
function helper() {
return 'helping';
}
const MAX_SIZE = 100;
export { helper, MAX_SIZE };
Named Exports
Every export shown above is a named export — the importing module must use the exact name to access it. You can export as many named values as you want from a single module.
Why This Matters
Without export, your module's code would be completely isolated. Export transforms private module code into a public API that other parts of your application can depend on. You're defining what your module "offers" to the rest of your codebase.
Key Takeaway: The export keyword makes values from a module accessible to other modules — without it, everything in a module remains private.