The Global Object (window & globalThis) in JavaScript
The global object hosts globals: window in browsers, global in Node, and globalThis everywhere as the portable spelling. Top-level var declarations and function declarations become its properties (var x = 1 makes window.x), while let/const live in a separate declarative record β global but not properties, which is why let avoids clobbering window APIs.
Best practice minimizes direct use: modules scope code away from globals, and explicit globalThis access beats implicit window references for isomorphic code. Polluting globals collides across scripts β the original sin strict mode and modules were built to prevent.
Three names, one idea
window (browsers), global (Node), globalThis (everywhere) β the shared top-level namespace and home of setTimeout, fetch, console.
var attaches, let doesn't
Top-level var x creates window.x; top-level let x does not β another reason let avoids stepping on window APIs.
Use globalThis portably
globalThis.setTimeout works in browsers and Node alike β no environment sniffing for shared utilities.
Stay out of it
Modules, closures, and explicit exports keep names local β globals are for runtimes, not applications.
var legacy = "V";let modern = "L";console.log(globalThis.legacy, globalThis.modern);Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself β building beats watching.