Export it from one module and import it in another. Top-level variables in a module are private by default. Only explicitly exported values are accessible to importers.
Scope in ES Modules vs Scripts in JavaScript
How do you share a variable across ES modules?
The answer
More FAQs in Scope in ES Modules vs Scripts in JavaScript
In classic scripts, top-level var becomes a property of window and this is the global object. In ES modules, top-level variables are module-scoped (not global), this is undefined, and strict mode is automatic.
No. In ES modules, top-level var, let, const, and class are module-scoped. They are not properties of window and are only accessible outside the module if explicitly exported.
undefined. In classic scripts, top-level this is the global object (window). In ES modules, it is undefined to prevent accidental global mutation. Use globalThis if you need the global object.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.