Sub topic 10
Top-Level Variables and globalThis
This chapter assumes you know how var, let, and const differ in scope, and that you can run code in a browser console or the Node.js REPL. It covers one more difference between the keywords: what happens when you declare a variable at the top level of a script, outside any function or block.
The Global Object
Every JavaScript environment has a global object. In browsers it is window, in Node.js it is global, and in Web Workers it is self. Since ES2020, globalThis refers to the global object in every environment, so portable code should use globalThis.
Properties of the global object can be read as plain variable names. For example, globalThis.console is what makes console available everywhere.
var Writes to globalThis, let and const Do Not
In a classic script, a top-level var declaration creates a property on the global object. A top-level let or const creates a global variable that every script can see, but it is not attached to globalThis.
// Run as a classic script (not an ES module)
var g = 1;
let h = 2;
console.log(globalThis.g); // 1: top-level var becomes a property of the global object
console.log(globalThis.h); // undefined: top-level let/const do not
console.log(h); // 2: h is still a global variable, just not a propertyTop-level function declarations behave like var and also become properties of the global object. Top-level class declarations behave like let.
How the Global Scope Is Built
The specification splits the global environment into two parts. The object record is backed by the global object itself and holds var and function declarations. The declarative record is a separate store that holds let, const, and class declarations. When you look up a name, the engine checks the declarative record first and then the object record.
Why the Difference Matters
Collisions with built-in properties
Because var writes to the global object, it can collide with properties the host already defines. In a browser, window.name is a built-in property whose setter converts every value to a string:
// Browser console, classic script
var name = 42;
console.log(typeof name); // 'string': you assigned to window.nameRun the same code with let name = 42; in a fresh page and typeof name is 'number', because the let binding lives in the declarative record and shadows window.name instead of writing to it.
var globals cannot be deleted
A property created by var is non-configurable, so you cannot delete it. A property you assign directly to globalThis can be deleted.
// Run as a classic script in sloppy mode
var fixed = 1;
globalThis.loose = 2;
console.log(delete globalThis.fixed); // false: var-created property is non-configurable
console.log(delete globalThis.loose); // true
console.log(typeof loose); // 'undefined'In strict mode, the failed delete throws a TypeError instead of returning false.
Conflicts across scripts
All classic scripts on a page share one global scope. If one script declares let config and another script declares var config or let config, the second script fails with a SyntaxError before any of its code runs. With var in both scripts, the second declaration silently reuses the same property, which can be worse because nothing warns you.
Modules and Node.js Files Are Different
None of this applies to ES modules. A module has its own top-level scope, so var, let, and const at the top of a module stay inside that module and never reach globalThis. The same is true of Node.js CommonJS files, because Node wraps each file in a function before running it.
| Context | Top-level `var` on `globalThis`? | Top-level `let`/`const` on `globalThis`? |
|---|---|---|
| Browser classic script | Yes | No |
| Node.js REPL | Yes | No |
| ES module (browser or Node) | No | No |
| Node.js CommonJS file | No | No |
Exercises
Exercise
In a browser classic script, you run var status = 404; let code = 404;. What do typeof globalThis.status and typeof globalThis.code print, and why?
Show answer
typeof globalThis.status prints 'string'. The top-level var assigns to the built-in window.status property, which converts values to strings. typeof globalThis.code prints 'undefined', because a top-level let creates a binding in the declarative record and does not add a property to the global object.
Exercise
A file contains var counter = 0; console.log(globalThis.counter);. It prints 0 in the Node.js REPL but undefined when run as node app.js. Explain the difference.
Show answer
In the REPL, top-level var adds a property to the global object. When run as a CommonJS file, Node wraps the code in a function, so counter is function-scoped to that wrapper and never becomes a property of globalThis.