SyntaxStudy
Sign Up
JavaScript Module Scope and Privacy
JavaScript Intermediate 5 min read

Module Scope and Privacy

Module Scope

Variables declared in a module are private to that module by default. Only explicitly exported values are accessible to other modules.

Example
// 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!
Pro Tip

Module scope is the native JS equivalent of the revealing module pattern — private by default, public by choice.