Learn JavaScript
❯ variables.global_object

The Global Object (window & globalThis) in JavaScript

$concept

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.

$how it works
1

Three names, one idea

window (browsers), global (Node), globalThis (everywhere) β€” the shared top-level namespace and home of setTimeout, fetch, console.

2

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.

3

Use globalThis portably

globalThis.setTimeout works in browsers and Node alike β€” no environment sniffing for shared utilities.

4

Stay out of it

Modules, closures, and explicit exports keep names local β€” globals are for runtimes, not applications.

$example
var legacy = "V";
let modern = "L";
console.log(globalThis.legacy, globalThis.modern);
Output
V undefined
1var legacy attaches to the global object β€” reachable as globalThis.legacy.
2let modern stays in the declarative record β€” globalThis.modern is undefined despite being global.
3V undefined prints β€” the asymmetry that keeps let declarations from colliding with window properties.
$use cases
Writing isomorphic code against globalThis instead of window
Understanding why let avoids clobbering window event/name/status
Reading legacy scripts through their window-attached globals
Keeping application state in modules, never on the global object

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself β€” building beats watching.