Sub topic 11
Accidental Globals and Strict Mode
This chapter assumes you already know how var, let, and const are scoped, and that a top-level var in a classic script becomes a property of globalThis. Here we look at a related trap: creating a global variable without declaring it at all.
What an accidental global is
An accidental global (also called an implicit global) is a variable that comes into existence because you assigned to a name that was never declared. JavaScript looks up the name in every enclosing scope, one after another. If it finds no binding anywhere and the code runs in sloppy mode, JavaScript creates a new property on the global object instead of reporting an error.
Sloppy mode is the informal name for JavaScript's default, non-strict behavior. Strict mode is an opt-in set of rules that turns several silent mistakes into thrown errors.
function sloppy() {
total = 42; // no declaration: creates a global in sloppy mode
}
sloppy();
console.log(globalThis.total); // 42
function strict() {
'use strict';
missing = 1; // ReferenceError in strict mode
}
try {
strict();
} catch (e) {
console.log(e.name); // 'ReferenceError'
}Run this as a classic script or a CommonJS file in Node.js. The first function quietly leaks total into the global object. The second function throws as soon as it assigns to missing.
How accidental globals sneak in
Nobody leaks globals on purpose. These three patterns cause most of them.
| Pattern | Example | What leaks |
|---|---|---|
| Forgotten keyword | `count = 0;` inside a function | `count` |
| Typo in a name | `totl = total + 1;` | `totl` |
| Chained assignment | `let a = b = 0;` | `b` |
The chained case catches experienced developers too. Assignment groups right to left, so let a = b = 0 means let a = (b = 0). Only a is declared. b is a plain assignment to an undeclared name.
function setup() {
let a = b = 0;
return a;
}
setup();
console.log(typeof b); // 'number' in sloppy mode: b leaked to the global scopeAccidental globals cause bugs that are hard to trace. Two unrelated functions can share the same leaked name without either one knowing. A typo creates a new variable and leaves the one you meant unchanged. Nothing fails at the line where the mistake happened.
Turning on strict mode
You enable strict mode by putting the directive 'use strict'; as the first statement of a script or a function.
'use strict';
function update() {
let total = 0;
totl = total + 1; // typo
}
try {
update();
} catch (e) {
console.log(e.name, e.message); // ReferenceError totl is not defined
}The directive counts only when it is the first statement of the script or function. If it appears later, JavaScript treats it as an ordinary string expression and ignores it.
Some code is strict automatically, with no directive:
- ES modules: files loaded with
import,<script type="module">, or.mjsfiles in Node.js. - All code inside a
classbody.
Implicit globals differ from var globals
A top-level var and an implicit global both end up as properties of globalThis, but they are not the same thing. A var declaration creates a property that cannot be deleted. An implicit global is an ordinary property, so delete removes it.
// Classic script, sloppy mode
var declared = 1;
leaked = 2;
console.log(delete globalThis.declared); // false
console.log(delete globalThis.leaked); // true
console.log(typeof leaked); // 'undefined'The difference rarely matters in practice. The real fix is to stop creating implicit globals.
Practical defenses
- Always declare variables with
constorlet. - Write new code as ES modules so strict mode is on by default.
- Use a linter. ESLint's
no-undefrule flags undeclared names before the code runs. - Avoid chained assignments in declarations. Write
let a = 0; let b = 0;instead.
Exercise
In a sloppy-mode classic script, what does this code log, and what changes if you add 'use strict'; as the first line? function init() { var x = y = 5; } init(); console.log(typeof x, typeof y);
Show answer
In sloppy mode it logs "undefined number". x is function-scoped to init, but y was never declared, so the assignment y = 5 creates a global. With 'use strict'; at the top, calling init() throws a ReferenceError at y = 5, because strict mode forbids assigning to an undeclared name. The console.log line never runs.
Exercise
A teammate says, "Our code is in .mjs files, so we never need 'use strict'." Are they right? Why?
Show answer
Yes. Files ending in .mjs are ES modules, and ES modules always run in strict mode. Adding the directive changes nothing. Assigning to an undeclared name in those files already throws a ReferenceError.