TExcelTAD
Home › JavaScript › JS Strict Mode

JavaScript Strict Mode

Strict mode is an opt-in, safer variant of JavaScript introduced in ES5. It turns several silent mistakes into real errors, removes a few confusing features, and makes code easier for engines to optimise.

Turning it on

Write the string "use strict"; as the very first statement of a script or a function:

js
"use strict";             // whole script is strict

function legacyHelper() {
  // ...
}

function strictHelper() {
  "use strict";           // only this function is strict
  // ...
}

But in modern code you rarely need to write it, because strict mode is automatic in:

  • ES modules (<script type="module">, files using import/export)
  • class bodies
  • code compiled by most bundlers and TypeScript

Note: Every JavaScript playground in this course runs as an ES module, so your code is always strict here. To show the old "sloppy" behaviour below, we use new Function(...), which creates a function that is non-strict unless its body says otherwise.

Mistake 1: accidental globals

In sloppy mode, assigning to a variable you forgot to declare silently creates a global variable — typos become hidden bugs. Strict mode throws a ReferenceError instead:

js · try it yourself
// sloppy: the typo creates a global variable
const sloppy = new Function(`
  countr = 5;                 // typo for "counter"
  return typeof countr;
`);
console.log("sloppy:", sloppy());

// strict (our module): the same typo is an error
try {
  countr2 = 5;
} catch (err) {
  console.log("strict:", err.name, "-", err.message);
}
sloppy: number
strict: ReferenceError - countr2 is not defined

Mistake 2: silent failed assignments

Writing to a read-only property, adding a property to a frozen object, or assigning to a getter-only property is silently ignored in sloppy mode. Strict mode throws a TypeError, so you notice:

js · try it yourself
const frozen = Object.freeze({ level: 1 });

const sloppyWrite = new Function("obj", "obj.level = 99; return obj.level;");
console.log("sloppy result:", sloppyWrite(frozen));   // ignored silently

try {
  frozen.level = 99;
} catch (err) {
  console.log("strict:", err.message);
}
sloppy result: 1
strict: Cannot assign to read only property 'level' of object '#<Object>'

Mistake 3: this in plain function calls

In sloppy mode, a function called without an object gets the global object as this — so code meant for an object might accidentally read or write global variables. In strict mode, this is undefined:

js · try it yourself
const sloppyThis = new Function("return typeof this;");
function strictThis() {
  return typeof this;
}
console.log("sloppy:", sloppyThis());
console.log("strict:", strictThis());
sloppy: object
strict: undefined

Other strict-mode rules

Not allowed in strict mode Example
Duplicate parameter names function f(a, a) {} → SyntaxError
with statements with (obj) { … } → SyntaxError
Deleting plain variables delete x; → SyntaxError
Octal literals with a leading zero 010 → SyntaxError (use 0o10)
Using reserved words as names let interface;, let package; → SyntaxError
eval creating variables in the surrounding scope eval("var x = 1") stays inside eval
arguments.callee throws a TypeError

Some of these are syntax errors, detected before any code runs:

js · try it yourself
try {
  new Function('"use strict"; function f(a, a) {}');
} catch (err) {
  console.log(err.name + ": " + err.message);
}
try {
  new Function('"use strict"; var n = 010;');
} catch (err) {
  console.log(err.name + ": " + err.message);
}
SyntaxError: Duplicate parameter name not allowed in this context
SyntaxError: Octal literals are not allowed in strict mode.

Should you use it?

Yes — and you probably already do. Write your code as ES modules or classes and you get strict mode for free. Only very old code (classic scripts without "use strict") still runs in sloppy mode, and when you maintain such code, adding "use strict"; is usually a quick win that surfaces hidden bugs.

Test yourself

Which code is strict automatically?

  1. Every classic <script>
  2. ES modules and class bodies
  3. Only code in functions
  4. Code that uses var

In strict mode, what is this inside a plain function call f()?

  1. The global object
  2. undefined
  3. The function itself
  4. null

What happens in strict mode when you assign to an undeclared variable?

  1. A global variable is created
  2. The assignment is ignored
  3. A ReferenceError is thrown
  4. It becomes a local variable