CKTIS / JavaScript
Strict mode directive in JavaScript
The `"use strict"` directive opts a classic script or a function into strict mode. ECMAScript modules and class definitions are already strict.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
"use strict";
'use strict';The Strict mode directive is the exact statement "use strict"; or 'use strict'; in the directive prologue of a classic script or function body. The prologue is the initial run of statements each consisting of only a string literal and a semicolon, which may be inserted automatically. Comments and earlier statements of that form do not end the prologue. Use the exact spelling without escapes or line continuations. A parenthesized string, a string after a declaration, or a string in an ordinary {} block does not enable strict mode.
Strictness follows where code is defined, not where it is called. Functions defined in strict code inherit it; separate classic scripts do not. ECMAScript modules (a browser script element with type="module") and class declarations and expressions are always strict. A directive in a function's own body is an early SyntaxError if the function uses default, rest, or destructured parameters, even in already-strict code. Omit it if the enclosing code is strict, or enable strict mode in an enclosing script or function instead.
In strict code, a with statement causes a SyntaxError before execution. Assigning to an unresolvable name throws ReferenceError at runtime instead of creating a global property, and writing to a read-only property throws TypeError instead of silently failing. In an ordinary strict function called without a receiver, this is undefined rather than the global this value. Arrow functions still take this from their lexical scope.
A direct eval call in strict code inherits strictness. Indirect eval and the Function constructor do not inherit it from the caller; the supplied source can contain its own directive. Strict mode is not a security sandbox or a guarantee that the runtime supports modules.
Examples
(function () {
"use strict";
const settings = Object.freeze({ limit: 1 });
console.log(this === undefined);
try {
settings.limit = 2;
} catch (error) {
console.log(error.name);
}
console.log(settings.limit);
})();Result
true TypeError 1
The function is called without a receiver, so `this === undefined` is `true`. `Object.freeze` makes `limit` read-only. The `catch` block logs the error name `TypeError`, and `limit` remains `1`.
Related lessons
- JavaScript · Lesson 20 · Advanced · Version: 4
- JavaScript · Lesson 21 · Advanced · Version: 4
- JavaScript · Lesson 22 · Advanced · Version: 4
- JavaScript · Lesson 23 · Advanced · Version: 4
- JavaScript · Lesson 24 · Advanced · Version: 4
- JavaScript · Lesson 25 · Advanced · Version: 4
- JavaScript · Lesson 26 · Advanced · Version: 4
- JavaScript · Lesson 27 · Advanced · Version: 4
- JavaScript · Lesson 28 · Advanced · Version: 4
- JavaScript · Lesson 29 · Advanced · Version: 4
- JavaScript · Lesson 30 · Advanced · Version: 4
- JavaScript · Lesson 31 · Advanced · Version: 4
- JavaScript · Lesson 32 · Advanced · Version: 4
- JavaScript · Lesson 33 · Advanced · Version: 4
- JavaScript · Lesson 34 · Advanced · Version: 4
- JavaScript · Lesson 35 · Advanced · Version: 4
- JavaScript · Lesson 36 · Advanced · Version: 4
- JavaScript · Lesson 37 · Advanced · Version: 4
- JavaScript · Lesson 38 · Advanced · Version: 4
- JavaScript · Lesson 39 · Advanced · Version: 4
- JavaScript · Lesson 40 · Advanced · Version: 4
- JavaScript · Lesson 41 · Advanced · Version: 4
- JavaScript · Lesson 42 · Advanced · Version: 4
- JavaScript · Lesson 43 · Advanced · Version: 4
- JavaScript · Lesson 44 · Advanced · Version: 4
- JavaScript · Lesson 45 · Advanced · Version: 4
Sources
- ECMAScript 2024 §11.2.1 — Directive Prologues and the Use Strict Directive
- ECMAScript 2024 §11.2.2 — Strict Mode Code
- ECMAScript 2024 §15.2 — Function Definitions
- ECMAScript 2024 Annex C — The Strict Mode of ECMAScript
- ECMAScript 2024 §6.2.5.6 — PutValue
- ECMAScript 2024 §10.2.1.2 — OrdinaryCallBindThis
- ECMAScript 2024 §20.1.2.6 — Object.freeze
- HTML Standard — The script element: type attribute