let, const, and the Global Object in JavaScript
let and const at the top level do not become properties of window. Here is why and how it differs from var.
let, const, and the Global Object in JavaScript
A subtle difference between var and let/const at the top level: var becomes a property of the global object (window), but let and const do not.
var at the Top Level
var x = 5; console.log(window.x); // 5 (in a browser, non-module script) `` `var` declarations at the top level become properties of `window`. This is a legacy behavior from pre-ES6. ### `let` and `const` at the Top Level ```js let y = 10; const z = 15; console.log(window.y); // undefined console.log(window.z); // undefined `` `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`. ### Why the Difference Matters - `window` is mutable and shared. Polluting it with `var` globals causes collisions and bugs. - `let`/`const` globals are still global, but they do not attach to `window`, which is cleaner. - In ES modules, top-level `let`/`const`/`var` are all module-scoped, not global at all. ### Checking for Globals ```js // var global var a = 1; "a" in window; // true // let global let b = 2; "b" in window; // false typeof b !== "undefined"; // true (b exists, just not on window) `` ### In Node.js In Node.js, top-level `var` in a CommonJS module does **not** become a property of `global` (unlike browsers). It is scoped to the module. Top-level `let`/`const` are also module-scoped. ### In ES Modules In ES modules (browser or Node), top-level `var`, `let`, and `const` are all module-scoped. None become properties of the global object. This is the cleanest behavior. ### The Takeaway `var` at the top level becomes a property of `window` (in browser non-module scripts). `let` and `const` do not; they live in a separate global lexical environment. In ES modules, all top-level declarations are module-scoped. Prefer `let`/`const` and modules to avoid global pollution.
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.
Use typeof. typeof b !== 'undefined' checks if b exists without throwing, even though b is not a property of window. Do not use 'b' in window, because let globals do not attach to window.
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.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

