Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Strict mode — use strict

Strict mode changes JavaScript syntax and execution rules, exposing some errors that would otherwise be ignored. Enable it with a "use strict" directive; modules and class code are automatically strict.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

"use strict";

function example() {
  "use strict";
}

The directive "use strict"; or 'use strict'; in the initial sequence of string-literal expression statements enables strict mode for a script or function, including nested functions. It must not contain escape sequences. Placing it after an ordinary statement or inside an arbitrary block does not enable strict mode.

Modules and all parts of class declarations and expressions are automatically strict. The directive is not a global switch for other, independent scripts.

Assignment to an unresolvable identifier throws ReferenceError rather than creating a global-object property. Writing to a non-writable data property throws TypeError rather than being silently ignored. In a plain call such as readThis(), this inside a strict ordinary function is undefined; this does not change arrow-function this behavior.

Do not put an explicit directive inside a function with default, rest, or destructured parameters: doing so is a SyntaxError. Such a function can still inherit strict mode from surrounding code. Strict mode also forbids duplicate parameter names and the with statement.

Examples

(function () {
  "use strict";

  function readThis() {
    return this;
  }

  const record = {};
  Object.defineProperty(record, "count", {
    value: 1,
    writable: false
  });

  console.log(readThis() === undefined);
  try {
    record.count = 2;
  } catch (error) {
    console.log(error instanceof TypeError);
  }
  console.log(record.count);
})();

Result

true
true
1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources