Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Classes and Modules

Strict mode

Concept page on JavaScript strict mode: how to turn it on, which silent mistakes become errors, how this changes, and where code is strict automatically.

Strict mode is a restricted variant of JavaScript. It does not add features; it takes away a few risky behaviors and turns errors that used to be silent into exceptions you can see. Code that runs without strict mode is called sloppy mode code.

You do not usually need to switch it on by hand. ES modules and the bodies of classes are strict automatically, and most tooling emits strict code. Knowing the differences still matters, because they explain error messages and why the same snippet behaves differently in a module and in an old script.

JavaScript
"use strict";            // whole script

function f() {
  "use strict";          // this function only
}

Try it

JS Silent failures become errors

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

No output.

JS this and arguments

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

No output.

Turning it on

  • Script or function: put the string "use strict" as the first statement. See the "use strict" directive.
  • Modules: every ES module is strict, with no directive needed. See ES modules.
  • Classes: code inside a class body, including methods and field initializers, is always strict.
  • eval: code passed to eval() is strict if the calling code is, or if the string starts with its own directive.
  • Function constructor: the function body is sloppy unless it begins with the directive.

There is no way to turn strict mode off again inside a strict region.

What changes

Silent failures throw

In sloppy mode some operations quietly do nothing. In strict mode they throw a TypeError:

  • assigning to a read-only property, a getter-only property, or a property of a frozen object,
  • adding a property to a non-extensible (sealed, frozen or prevented) object,
  • deleting a non-configurable property,
  • setting a property on a primitive such as "text".length = 1.

Assigning to a name that was never declared throws a ReferenceError instead of creating a global variable.

this

In a plain function call this is undefined, not the global object. When you call a function with call(), apply() or bind(), a primitive this is passed as is instead of being wrapped in an object. This catches the classic bug of detaching a method from its object.

Syntax that becomes an error

These are early SyntaxErrors in strict code: the with statement, duplicate parameter names, legacy octal literals such as 010 and octal escapes such as "\010", deleting a plain variable with delete name, and using eval or arguments as a name.

The words implements, interface, let, package, private, protected, public, static and yield cannot be used as identifiers, which leaves room for newer language features.

Cleaner function behavior

  • arguments no longer stays linked to named parameters, and arguments.callee throws.
  • A function declaration inside a block is scoped to that block.
  • Variables declared inside eval() stay inside it instead of leaking into the surrounding scope.

Usage notes

  • Turn it on for scripts. If you still write classic scripts, start with "use strict" at the top of each file or wrap code in a function that has it. Modules need nothing.
  • Beware concatenation. A file-level directive in one script can make a joined sloppy script fail. Prefer modules or function-level directives.
  • It is not a sandbox or a performance switch. It prevents mistakes; any speedup is incidental.
  • Moving old code into a module can reveal hidden bugs: accidental globals, a this that used to be window, and writes to read-only values.
  • Linters and TypeScript catch most of the same mistakes before the code runs.

Browser support

Strict mode is supported in all current browsers and runtimes. Very old engines ignore the directive and run the code in sloppy mode.