JavaScript · Scope · Mid-Level · Comparison
How does module scope differ from classic script global scope?
Short Interview Answer
ES modules have their own top-level scope; top-level bindings do not become properties of `globalThis`, and modules are strict by default.
Detailed Explanation
A classic `<script>` (non-module) shares the global object: top-level `var` and function declarations become `globalThis` properties. An ES module has a Module Environment Record: top-level `let`/`const`/`class`/`function`/`var` stay module-private unless exported. Modules always run in strict mode, support `import`/`export`, and are deferred by default when loaded via `type="module"`. This isolation reduces global namespace collisions and makes dependencies explicit. Cross-module sharing happens only through exports or intentional `globalThis` assignment.
Interview Tip
Mention that duplicate `import` of the same module yields a singleton evaluation — ties scope to the module graph.
Common Mistake
Assuming `const x = 1` at the top of a module is available as `window.x`.