ES Modules
ES modules provide a standardized module system using import and export. Modules have their own top-level scope, execute in strict mode, and can expose selected bindings.
Named exports and default exports have different semantics. Named exports are often preferable when a module exposes several independent concepts because import names can communicate intent.
Named and Default Exports
export function add(a, b) {
return a + b;
}
export const version = '1.0.0';
// another file
import { add, version } from './math.js';💡 The module exports named bindings. Another module imports those bindings explicitly.
Key Takeaways
- ES modules create their own scope.
- Modules execute in strict mode.
- Named exports communicate specific module capabilities.
Knowledge Check
What keyword is used to expose a binding from an ES module?