Sub topic 13
Accidental Globals and Strict Mode
This chapter assumes you know how the scope chain resolves names and how var behaves at the top level of a script. It covers one quirk of name resolution: what happens when you assign to a name that was never declared.
Reading Versus Writing an Undeclared Name
When JavaScript reads a name, it walks the scope chain outward. If it reaches the global scope without finding the name, it throws a ReferenceError. That rule is the same in every mode.
Writing to a name works differently. In sloppy mode, which is the default for classic scripts and CommonJS files in Node.js, an assignment that finds no declaration does not fail. The engine creates a new property on the global object instead. The result is called an implicit global, or accidental global.
function leakSloppy() {
leaked = 1; // no declaration
}
leakSloppy();
console.log(globalThis.leaked); // 1
function leakStrict() {
"use strict";
try {
alsoLeaked = 1;
} catch (e) {
console.log(e.name); // "ReferenceError"
}
}
leakStrict();Inside leakSloppy, leaked belongs to no scope. The assignment reaches the global scope and creates globalThis.leaked, which stays there after the function returns. Inside leakStrict, the same assignment throws.
Why Accidental Globals Hurt
Most accidental globals come from typos or a missing let. The code keeps running, so the bug shows up somewhere else.
function total(items) {
let sum = 0;
for (const item of items) {
summ = sum + item; // typo: summ instead of sum
}
return sum;
}
console.log(total([1, 2, 3])); // 0
console.log(globalThis.summ); // 3The function returns 0 without any error, and a stray summ now sits on the global object. Any other code using that name can read or overwrite it. Two functions that leak the same name will also overwrite each other's values without warning.
An implicit global also differs from a declared one:
| Created by | Property on global object | Can be removed with `delete` |
|---|---|---|
| Top-level `var x` in a script | Yes | No |
| Sloppy assignment `x = 1` | Yes | Yes |
| Top-level `let x` or `const x` | No | No (not a property) |
Turning On Strict Mode
Strict mode changes several sloppy behaviors into errors. For scope, the change that matters is that assigning to an undeclared name throws a ReferenceError. You enable it in any of these ways:
- Put
"use strict";as the first statement of a script file. The whole file becomes strict. - Put
"use strict";as the first statement of a function body. Only that function and the functions nested in it become strict. - Use an ES module. Module code is always strict.
- Write code inside a
classbody. Class bodies are always strict.
"use strict";
function total(items) {
let sum = 0;
for (const item of items) {
summ = sum + item; // ReferenceError: summ is not defined
}
return sum;
}
try {
total([1, 2, 3]);
} catch (e) {
console.log(e.name); // "ReferenceError"
}Now the typo fails on the line where it happens. Linters such as ESLint, with its no-undef rule, catch the same mistake before the code runs.
Exercise
In a sloppy-mode script, function f() { count = 5; } f(); console.log(count); prints 5. What does it print, or throw, if you add "use strict"; as the first line of f's body, and why?
Show answer
Calling f throws a ReferenceError, so the console.log line never runs. In strict mode an assignment to a name with no declaration on the scope chain is an error, so no global property is created.
Exercise
Reading an undeclared name throws a ReferenceError even in sloppy mode. Why does assigning to one not throw in sloppy mode?
Show answer
Sloppy mode treats an unresolved assignment as a request to create a property on the global object. Only reads fail when the scope chain search finds nothing. Strict mode removes that fallback, so reads and writes of unresolved names both throw.