1 # Disallow Unused Expressions (no-unused-expressions)
3 An unused expression which has no effect on the state of the program indicates a logic error.
5 For example, `n + 1;` is not a syntax error, but it might be a typing mistake where a programmer meant an assignment statement `n += 1;` instead. Sometimes, such unused expressions may be eliminated by some build tools in production environment, which possibly breaks application logic.
9 This rule aims to eliminate unused expressions which have no effect on the state of the program.
11 This rule does not apply to function calls or constructor calls with the `new` operator, because they could have *side effects* on the state of the program.
15 function increment() { i += 1; }
16 increment(); // return value is unused, but i changed as a side effect
19 function Thing() { nThings += 1; }
20 new Thing(); // constructed object is unused, but nThings changed as a side effect
23 This rule does not apply to directives (which are in the form of literal string expressions such as `"use strict";` at the beginning of a script, module, or function).
25 Sequence expressions (those using a comma, such as `a = 1, b = 2`) are always considered unused unless their return value is assigned or used in a condition evaluation, or a function call is made with the sequence expression value.
29 This rule, in its default state, does not require any arguments. If you would like to enable one or more of the following you may pass an object with the options set as follows:
31 * `allowShortCircuit` set to `true` will allow you to use short circuit evaluations in your expressions (Default: `false`).
32 * `allowTernary` set to `true` will enable you to use ternary operators in your expressions similarly to short circuit evaluations (Default: `false`).
33 * `allowTaggedTemplates` set to `true` will enable you to use tagged template literals in your expressions (Default: `false`).
34 * `enforceForJSX` set to `true` will flag unused JSX element expressions (Default: `false`).
36 These options allow unused expressions *only if all* of the code paths either directly change the state (for example, assignment statement) or could have *side effects* (for example, function call).
38 Examples of **incorrect** code for the default `{ "allowShortCircuit": false, "allowTernary": false }` options:
41 /*eslint no-unused-expressions: "error"*/
57 a() && function namedFunctionInExpressionContext () {f();}
59 (function anIncompleteIIFE () {});
61 injectGlobal`body{ color: red; }`
65 Examples of **correct** code for the default `{ "allowShortCircuit": false, "allowTernary": false }` options:
68 /*eslint no-unused-expressions: "error"*/
70 {} // In this context, this is a block statement, not an object literal
72 {myLabel: someVar} // In this context, this is a block statement with a label and expression, not an object literal
74 function namedFunctionDeclaration () {}
76 (function aGenuineIIFE () {}());
89 Note that one or more string expression statements (with or without semi-colons) will only be considered as unused if they are not in the beginning of a script, module, or function (alone and uninterrupted by other statements). Otherwise, they will be treated as part of a "directive prologue", a section potentially usable by JavaScript engines. This includes "strict mode" directives.
91 Examples of **correct** code for this rule in regard to directives:
94 /*eslint no-unused-expressions: "error"*/
100 "any other strings like this in the directive prologue";
101 "this is still the directive prologue";
114 Examples of **incorrect** code for this rule in regard to directives:
117 /*eslint no-unused-expressions: "error"*/
120 "use strict"; // this isn't in a directive prologue, because there is a non-directive statement before it
128 "use strict"; // class static blocks do not have directive prologues
133 ### allowShortCircuit
135 Examples of **incorrect** code for the `{ "allowShortCircuit": true }` option:
138 /*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/
143 Examples of **correct** code for the `{ "allowShortCircuit": true }` option:
146 /*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/
154 Examples of **incorrect** code for the `{ "allowTernary": true }` option:
157 /*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
163 Examples of **correct** code for the `{ "allowTernary": true }` option:
166 /*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
172 ### allowShortCircuit and allowTernary
174 Examples of **correct** code for the `{ "allowShortCircuit": true, "allowTernary": true }` options:
177 /*eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }]*/
179 a ? b() || (c = d) : e()
182 ### allowTaggedTemplates
184 Examples of **incorrect** code for the `{ "allowTaggedTemplates": true }` option:
187 /*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/
189 `some untagged template string`;
192 Examples of **correct** code for the `{ "allowTaggedTemplates": true }` option:
195 /*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/
197 tag`some tagged template string`;
202 JSX is most-commonly used in the React ecosystem, where it is compiled to `React.createElement` expressions. Though free from side-effects, these calls are not automatically flagged by the `no-unused-expression` rule. If you're using React, or any other side-effect-free JSX pragma, this option can be enabled to flag these expressions.
204 Examples of **incorrect** code for the `{ "enforceForJSX": true }` option:
207 /*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
214 Examples of **correct** code for the `{ "enforceForJSX": true }` option:
217 /*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
219 var myComponentPartial = <MyComponent />;
221 var myFragment = <></>;