1 # require or disallow strict mode directives (strict)
3 A strict mode directive is a `"use strict"` literal at the beginning of a script or function body. It enables strict mode semantics.
5 When a directive occurs in global scope, strict mode applies to the entire script:
17 When a directive occurs at the beginning of a function body, strict mode applies only to that function, including all contained functions:
37 In the **CommonJS** module system, a hidden function wraps each module and limits the scope of a "global" strict mode directive.
39 In **ECMAScript** modules, which always have strict mode semantics, the directives are unnecessary.
43 This rule requires or disallows strict mode directives.
45 This rule disallows strict mode directives, no matter which option is specified, if ESLint configuration specifies either of the following as [parser options](/docs/user-guide/configuring/language-options.md#specifying-parser-options):
47 * `"sourceType": "module"` that is, files are **ECMAScript** modules
48 * `"impliedStrict": true` property in the `ecmaFeatures` object
50 This rule disallows strict mode directives, no matter which option is specified, in functions with non-simple parameter lists (for example, parameter lists with default parameter values) because that is a syntax error in **ECMAScript 2016** and later. See the examples of the [function](#function) option.
52 This rule does not apply to class static blocks, no matter which option is specified, because class static blocks do not have directives. Therefore, a `"use strict"` statement in a class static block is not a directive, and will be reported by the [no-unused-expressions](no-unused-expressions.md) rule.
54 The `--fix` option on the command line does not insert new `"use strict"` statements, but only removes unneeded statements.
58 This rule has a string option:
60 * `"safe"` (default) corresponds either of the following options:
61 * `"global"` if ESLint considers a file to be a **CommonJS** module
62 * `"function"` otherwise
63 * `"global"` requires one strict mode directive in the global scope (and disallows any other strict mode directives)
64 * `"function"` requires one strict mode directive in each top-level function declaration or expression (and disallows any other strict mode directives)
65 * `"never"` disallows strict mode directives
69 The `"safe"` option corresponds to the `"global"` option if ESLint considers a file to be a **Node.js** or **CommonJS** module because the configuration specifies either of the following:
71 * `node` or `commonjs` [environments](/docs/user-guide/configuring/language-options.md#specifying-environments)
72 * `"globalReturn": true` property in the `ecmaFeatures` object of [parser options](/docs/user-guide/configuring/language-options.md#specifying-parser-options)
74 Otherwise the `"safe"` option corresponds to the `"function"` option. Note that if `"globalReturn": false` is explicitly specified in the configuration, the `"safe"` option will correspond to the `"function"` option regardless of the specified environment.
78 Examples of **incorrect** code for this rule with the `"global"` option:
81 /*eslint strict: ["error", "global"]*/
88 /*eslint strict: ["error", "global"]*/
96 /*eslint strict: ["error", "global"]*/
105 Examples of **correct** code for this rule with the `"global"` option:
108 /*eslint strict: ["error", "global"]*/
118 This option ensures that all function bodies are strict mode code, while global code is not. Particularly if a build step concatenates multiple scripts, a strict mode directive in global code of one script could unintentionally enable strict mode in another script that was not intended to be strict code.
120 Examples of **incorrect** code for this rule with the `"function"` option:
123 /*eslint strict: ["error", "function"]*/
132 /*eslint strict: ["error", "function"]*/
145 /*eslint strict: ["error", "function"]*/
148 // Illegal "use strict" directive in function with non-simple parameter list.
149 // This is a syntax error since ES2016.
150 function foo(a = 1) {
154 // We cannot write "use strict" directive in this function.
155 // So we have to wrap this function with a function with "use strict" directive.
156 function foo(a = 1) {
160 Examples of **correct** code for this rule with the `"function"` option:
163 /*eslint strict: ["error", "function"]*/
175 function baz(a = 1) {
179 var foo = (function() {
182 return function foo(a = 1) {
189 Examples of **incorrect** code for this rule with the `"never"` option:
192 /*eslint strict: ["error", "never"]*/
201 /*eslint strict: ["error", "never"]*/
208 Examples of **correct** code for this rule with the `"never"` option:
211 /*eslint strict: ["error", "never"]*/
217 ### earlier default (removed)
219 (removed) The default option (that is, no string option specified) for this rule was **removed** in ESLint v1.0. The `"function"` option is most similar to the removed option.
221 This option ensures that all functions are executed in strict mode. A strict mode directive must be present in global code or in every top-level function declaration or expression. It does not concern itself with unnecessary strict mode directives in nested functions that are already strict, nor with multiple strict mode directives at the same level.
223 Examples of **incorrect** code for this rule with the earlier default option which has been removed:
242 Examples of **correct** code for this rule with the earlier default option which has been removed:
272 ## When Not To Use It
274 In a codebase that has both strict and non-strict code, either turn this rule off, or [selectively disable it](/docs/user-guide/configuring/rules.md#disabling-rules) where necessary. For example, functions referencing `arguments.callee` are invalid in strict mode. A [full list of strict mode differences](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode/Transitioning_to_strict_mode#Differences_from_non-strict_to_strict) is available on MDN.