Sub topic 2
The Global Scope
This chapter assumes you can declare variables with var, let, and const, write functions, and run code in a browser console or the Node.js REPL. The previous chapter covered lexical scope in general. Here we look at the outermost scope, which every other scope sits inside.
What the Global Scope Is
The global scope is the scope at the top level of a classic script, outside any function or block. Any code in the program can read a name declared there, unless an inner scope declares the same name.
Every global scope comes with a global object. Its name depends on the environment:
| Environment | Global object name | Universal name |
|---|---|---|
| Browser main thread | `window` | `globalThis` |
| Web Worker | `self` | `globalThis` |
| Node.js | `global` | `globalThis` |
Use globalThis when you want code that works in any of these environments.
Two Kinds of Global Names
People often assume that every global variable is a property of the global object. That is only true for some declarations. Internally, the global scope keeps two separate stores of bindings:
vardeclarations and function declarations become properties of the global object.let,const, andclassdeclarations are still global, but they live in a separate store and do not appear on the global object.
Run this in a browser console or the Node.js REPL to see the difference:
var appName = "demo";
let version = 2;
function helper() {}
console.log(globalThis.appName); // "demo"
console.log(globalThis.helper === helper); // true
console.log(globalThis.version); // undefined
console.log(version); // 2All three names are global, and any code can read version by name. Only appName and helper are attached to globalThis.
One Global Scope, Many Scripts
In a browser, all classic <script> tags on a page share a single global scope. A name declared in one script is visible in every script that runs after it. This lets unrelated code collide.
A repeated var declaration overwrites the earlier value without any error. A repeated let or const is rejected. If the first script contains this:
let count = 0;and a later script contains this:
let count = 1; // SyntaxError: Identifier 'count' has already been declaredthen the second script fails to run at all. You get the error, but the second script is lost entirely.
Colliding with Built-in Globals
The global object already has many properties: name, status, top, location, and others in browsers. A top-level var with the same name reuses the existing property and its special behavior:
// In a classic browser script, at the top level
var name = 42;
console.log(typeof name); // "string"window.name always converts its value to a string, so your number becomes "42".
Keeping the Global Scope Small
Global names are easy to reach and easy to overwrite. Good habits:
- Declare top-level names with
letorconst, notvar. - Put code inside functions or modules so its names stay local.
- If you must expose something globally, attach a single namespace object, such as
globalThis.MyApp = {}, and hang everything off it.
A later chapter, Accidental Globals and Strict Mode, covers how assigning to an undeclared name creates a global by accident.
Exercise
In a browser console, you run var a = 1; const b = 2; at the top level. What do globalThis.a, globalThis.b, and b evaluate to, and why?
Show answer
globalThis.a is 1 because top-level var declarations become properties of the global object. globalThis.b is undefined because const bindings are global but stored outside the global object. b is 2 because code can still read the global const binding by name.
Exercise
Two classic scripts on the same page both declare var total at the top level. Then you change both to let total. What changes?
Show answer
With var, the second declaration reuses the same global property and no error occurs, which can hide the collision. With let, the second script throws "SyntaxError: Identifier 'total' has already been declared" and does not run, so the collision becomes visible.