Strict mode and the global object: turning silent mistakes into errors
Strict mode makes typos and failed writes throw instead of failing silently. Modules and classes are strict by default. Learn what changes, and globalThis.
Comes up in: JavaScript
Learn#
Lakshmi’s old script has totl = price * qty — a typo for total. Nothing fails. The page just shows the wrong number, and totl quietly appears as a global variable. She loses an afternoon.
That old forgiving behavior is now called sloppy mode. Strict mode, turned on with "use strict" at the top of a script or function, changes it. The three changes that matter most:
- Assigning to an undeclared name throws a
ReferenceErrorinstead of creating a global. - In a plain function call,
thisisundefinedinstead of the global object. - Writes that would fail silently — to a frozen object or a read-only property — throw a
TypeError.
ES modules and class bodies are always strict. So is this example, because it runs as a module:
const frozen = Object.freeze({ price: 100 });
try { frozen.price = 1; } catch (e) { console.log("frozen write:", e.name); }
try { totl = 5; } catch (e) { console.log("typo:", e.name); }
function plain() { return this; }
console.log("this in a plain call:", plain());
const sloppy = new Function("oops = 42; return typeof globalThis.oops;");
console.log("sloppy typo makes a global:", sloppy());
delete globalThis.oops;Output (verified)
frozen write: TypeError typo: ReferenceError this in a plain call: undefined sloppy typo makes a global: number
Code made with new Function is sloppy unless it says "use strict", which is why the last line still leaks.
global object
- (nothing named totl)
Console
empty
Sloppy mode: a typo, totl = 5, has no declaration anywhere in the scope chain.
Read the steps as text
- Sloppy mode: a typo, totl = 5, has no declaration anywhere in the scope chain.
- Sloppy mode quietly creates a new global variable instead of complaining.
- Strict mode: the same typo is not allowed. JavaScript throws at once, on the right line.
Predict#
Predict first
Inside an ES module: function f() { return this; } — what does f() return?
Modules are always strict, and in strict mode a plain call does not get a default this, so it is undefined. Answers of window come from old scripts in sloppy mode.
Interview angle#
Say it like this — about 30 seconds
“Strict mode turns silent mistakes into errors. Assigning to an undeclared variable throws instead of creating a global.
thisin a plain function call isundefined, not the global object. Writes to frozen or read-only properties throw. It also banswithand duplicate parameter names. You opt in with “use strict”, but ES modules and classes are strict automatically, so most modern code already is.”
Follow-up How do you enable strict mode?
Follow-up What is the global object called in different environments?
window in pages, self in workers, global in Node.js — and globalThis everywhere.Follow-up Can you leave strict mode inside a module?
new Function, can be sloppy.Practice#
On your own
Write trySet(obj, key, value). It must report honestly whether the write worked — the problem sloppy mode hides and strict mode turns into an exception.
Done when
- A normal object: sets the value, returns
true. - A frozen object, a read-only property or a getter-only property: returns
false, never throws.
Remember#
Try to answer each card before you open it. Then add them to your reviews.
What happens when strict code assigns to an undeclared variable?
It throws a ReferenceError instead of creating a global.
What is this in a plain function call in strict mode?
undefined.
Which code is always strict?
ES modules and class bodies.
What is globalThis?
The standard name for the global object in every environment.
Go deeper
Four layers. Stop at the one your interview needs; each ends with a quick check.
The core ideaEvery round
Strict mode trades forgiveness for honesty: mistakes throw where they happen. Modern code — modules, classes, most bundled code — already runs in it.
Check yourself: Is code in an ES module strict?
Yes, always.
How it works underneathSenior rounds
"use strict" is a directive: it only counts in the directive prologue, the first statements of a script or function Source: ECMA-262: Strict mode code. Anywhere else it is just an unused string. The spec’s Annex C lists every restriction Source: ECMA-262: The strict mode of ECMAScript.
Check yourself: Where must "use strict" appear to work?
As the first statement of a script or function body (a directive prologue).
Edge cases and trapsWhere people slip
A function with default, rest or destructured parameters cannot contain "use strict" — that is a SyntaxError. Concatenating a strict file with a sloppy one can change the meaning of either, which is one reason bundlers and modules replaced manual concatenation.
Check yourself: Can a function with default parameters contain "use strict"?
No. That is a SyntaxError.
In productionStaff-level answers
TypeScript adds "use strict" to non-module output when alwaysStrict is on, which strict: true turns on. For defensive writes, Reflect.set(obj, key, value) returns false instead of throwing, and Object.isFrozen tells you before you try.
Check yourself: Which API reports whether a property write succeeded, without throwing?
Reflect.set, which returns true or false.
Finished the five steps? Mark it, and its cards join your reviews.
Last checked 2026-10-31. Spot a mistake? Use “Report an error” below — fixes are listed on the corrections page.
Interview questions this answers
- What does strict mode change?