Table of Contents

About

strict mode makes some operations more strict such as:

  • the definition of this
  • the variable must be created implicitly
  • the data type

strict mode

ES5 introduced the strict mode. This feature allows you to opt in to a restricted version of JavaScript that disallows some of the more problematic or error-prone features of the full language.

Example

At the beginning of a scope

"use strict";

Within a function:

function f(x) {
    "use strict";
    .....
}

Evaluating a string literal in Js has no side effects, so an older engine such as ES3 engine executes the directive as an innocuous statement

strict with not strict

Strict and Non Strict mode can live together by wrapping the content in two immediately invoked function expressions (IIFEs).

Example:

// no strict-mode directive at the root
(function() {
    // file1.js
    "use strict";
    function f() {
        // ...
    }
    // ...
})();
(function() {
    // file2.js
    // no strict-mode directive
    function f() {
        var arguments = [];
        // ...
    }
    // ...
})();