The “strict mode” was introduced in ECMAScript 5. It allows you to put your script into the “strict” operating mode which prevents certain (potentially dangerous) operations.

ES6+ classes and native ECMAScript modules have strict mode enabled by default.

Strict mode features and limitations

JavaScript “use strict” statement imposes some restrictions on what you can do:

  1. No global variables
  2. Function parameter names must be unique (no duplicate parameters)
  3. Can’t introduce new variables with eval
  4. Static scoping
  5. No octal literals
  6. An attempt to delete a non-configurable property throws an error
  7. Using keywords like implements, interface, let, package, private, protected, public, static, or yield as variable names will throw an error

How to put your JS file in a strict mode

To put your *.js file into the strict mode, you should put the line “use strict” in the very first line of the file.

"use strict"

You can also put only a single code block (like a function) in a strict mode.

// regular js code
function strictFunction() {
  "use strict";
  // strict mode starts here
  // ...
  // and ends at the closing curly brace
}
// regular js code