Does var at the top level become a property of global in Node.js?
No. In Node.js CommonJS modules, top-level var is scoped to the module, not to global. This is different from browser non-module scripts where var becomes a property of window. In ES modules, all top-level declarations are module-scoped.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in let, const, and the Global Object in JavaScript
No. let and const at the top level live in a global lexical environment that is separate from the window object. They are still global (accessible everywhere in the script), but they are not properties of window. var does become a property of window.
It is a legacy behavior. var was designed before ES6 to attach to the global object. let and const were designed to be cleaner: they live in a separate global lexical environment and do not pollute window.
No. In ES modules, top-level let, const, and var are module-scoped, not global. They are only accessible outside the module if explicitly exported. This is the cleanest behavior and prevents global pollution.
Still have questions?
Browse all our FAQs or reach out to our support team
