The global object is the runtime object through which globally accessible values can be shared. In modern JavaScript, a module’s top-level scope is not global scope, so attach a value to the global object when other modules must access it without importing it. section 7.4
Listing 7.7 Attaching a value to the global object
// In globals.mts
declare global { #A
var ninjaName: string; #A
} #A
globalThis.ninjaName = "Naruto"; #B
// In main.mts
import "./globals.mts"; #C
console.log(`It's me, ${ninjaName}!`);
The declaration makes the global property available to TypeScript; the assignment creates it, allowing the importing module to use the name directly. section 7.4
Use `globalThis` to refer to the global object across environments. It was standardized in ECMAScript 2020; in browsers it equals `window`, and in Node.js it equals `global`. section 7.4
Avoid assuming that an undeclared assignment safely creates a global:
- In strict mode, assigning to an undeclared name throws a `ReferenceError`. section 7.4
- Without strict mode, such an assignment can create a global or overwrite an existing global property. section 7.4


