Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Statements and Declarations

The "use strict" directive

Reference for the JavaScript "use strict" directive: where to place it, script versus function scope, what it changes, and why modules and classes are already strict.

"use strict" is a directive prologue: a string literal at the very start of a script or function body that switches that code into strict mode. Strict mode turns silent mistakes into errors and removes a few confusing features. Old engines that do not know it simply treat it as an unused string.

JavaScript
"use strict";

function f() {
  // strict code
}

Try it

JS Function-level strict mode

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Other strict-mode errors

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
"use strict";   // or 'use strict';

The directive must be the first statement of a script or function body (only other directives or comments may precede it), and must be a plain string literal without escapes or parentheses.

Usage notes

  • Scope. At the top of a script it applies to the whole script; at the top of a function it applies only to that function and everything nested in it.
  • Already strict. ES modules and class bodies are strict automatically; adding the directive there is redundant.
  • What changes: assigning to an undeclared variable throws; writes to read-only properties and frozen objects throw instead of failing silently; this in plain function calls is undefined; duplicate parameter names and with are syntax errors; octal literals like 010 are errors; arguments no longer aliases the parameters.
  • Concatenation hazard. If you join scripts with a global directive, one strict script can make the following sloppy script fail. Use function-level directives or modules.
  • Not a security feature. It prevents mistakes; it does not isolate code.
  • Not retroactive. A directive that is not first has no effect, and a function with non-simple parameters (defaults, rest) cannot contain the directive.
  • Tools. Linters and TypeScript output strict code by default, so you rarely type the directive in modern projects.

Browser support

Strict mode is supported by every current browser and engine. Older environments ignore the string and run in sloppy mode.