Module Scope
Variables declared in a module are private to that module by default. Only explicitly exported values are accessible to other modules.
Variables declared in a module are private to that module by default. Only explicitly exported values are accessible to other modules.
// counter.js
let count = 0; // Private — not accessible outside
function increment() {
count++;
}
function decrement() {
count--;
}
function getCount() {
return count;
}
// Only export the public interface
export { increment, decrement, getCount };
// Importers cannot access `count` directly — encapsulation!
Module scope is the native JS equivalent of the revealing module pattern — private by default, public by choice.
More in JavaScript