Skip to content
  • Strict Mode · JavaScript

Strict Mode

LearnViaNotes @learnvianotes· 2 min read

"use strict" opts a script or function into a safer dialect that turns silent mistakes into errors. ES modules and class bodies are strict automatically.

Turning it on

JavaScript
"use strict";            // first statement of a script
function f() {
  "use strict";          // or of a single function
}

You rarely write it today: every ES module (import/export, .mjs, type="module") and every class body is strict whether or not the directive is present. Old-style scripts and CommonJS files are sloppy unless you opt in.

What changes

SituationSloppy modeStrict mode
Assign to undeclared variableCreates a globalReferenceError
this in a plain function callglobalThisundefined
Write to read-only / getter-only propertySilently ignoredTypeError
Add property to frozen / non-extensible objectSilently ignoredTypeError
delete a non-configurable propertyReturns falseTypeError
Duplicate parameter namesAllowedSyntaxError
with statementAllowedSyntaxError
Octal literal 0108SyntaxError (use 0o10)
arguments ↔ parametersLinked (aliasing)Independent
eval creates variables in the callerYesNo — own scope
Reserved future words (let, static, implements…)Usable as namesReserved

Seeing both modes side by side

This file is a module (strict). Code built with the Function constructor is sloppy unless it says otherwise — a handy way to compare:

JavaScript
const sloppy = new Function(`
  leaked = 1;                         // becomes a global
  const frozen = Object.freeze({ a: 1 });
  frozen.a = 2;                       // silently ignored
  return [typeof globalThis.leaked, frozen.a, this === globalThis];
`);
console.log("sloppy:", sloppy());

const strict = new Function(`
  "use strict";
  try { leaked2 = 1; } catch (e) { return e.name + " / this=" + this; }
`);
console.log("strict:", strict());

function args(a) { a = 99; return arguments[0]; }       // module → strict
console.log("arguments unlinked:", args(1));
Output
sloppy: [ 'number', 1, true ]
strict: ReferenceError / this=undefined
arguments unlinked: 1

What is strict mode and why use it?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report