Skip to content

Strict mode — the silent mistakes it turns into errors

Level: 201 · for anyone who has seen "use strict" at the top of a file

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: Without "use strict", assigning to a misspelled variable creates a global and writing to a frozen object does nothing; strict mode, which every module and class body gets automatically, turns the first into a ReferenceError and the second into a TypeError.

Keywords: use strict, strict mode, sloppy mode

What the finished page will answer

  • What happens to totl = 1 (a misspelled total) and to a write to a frozen property in sloppy mode, and in strict mode?
  • Which code is strict without any directive, and is a class body inside a sloppy function strict?
  • Why does "use strict" do nothing when it is not the first statement, and why is it a SyntaxError in a function with default parameters?
  • What is this in a plain function call in each mode, and what breaks in code that relied on it being globalThis?
  • Can TypeScript 7 compile a file as sloppy code at all, given that --alwaysStrict false is rejected with TS5108?

Examples it will need

  • strict_mode_silent_failures_cjs.cjs — a misspelled assignment, a write to a frozen object and an assignment to NaN, each silently doing nothing in sloppy mode
  • strict_mode_same_mistakes_js.js — the same three mistakes in a module, each caught with its ReferenceError or TypeError message
  • strict_mode_in_tsc_tserror.ts — tsc's diagnostics for a with statement (TS1101) and an assignment to an undeclared name (TS2304)

See also

Sources to start from