no-unused-expressions

Disallow statements that compute a value and throw it away

A statement such as server.close; or count === 0; computes a value and does nothing with it. It's usually a mistake, like a call that's missing its parentheses, or a === where you meant =.

Calls, new, assignments, await, yield, delete, void, i++, and directives such as "use strict" do something, so they aren't reported. Neither are isReady && start(); and isReady ? start() : wait();, which are short ways to write an if.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-unused-expressions" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-unused-expressions" = ["error", {
  allowShortCircuit = true,
  allowTernary = true,
  allowTaggedTemplates = false,
  enforceForJSX = false,
  ignoreDirectives = false,
}]

Examples#

example.js3 problems
server.close;
count === 0;
`Hello, ${name}`;
example.jsNo problems
"use strict";

server.close();
count = 0;
console.log(`Hello, ${name}`);

Options#

OptionDefaultWhat it does
allowShortCircuittrueAllow a && b(), a || b() and a ?? b(), if the right side would be allowed as a statement.
allowTernarytrueAllow a ? b() : c(), if both branches would be allowed as statements.
allowTaggedTemplatesfalseAllow a tagged template as a statement.
enforceForJSXfalseAlso report a JSX element as a statement.
ignoreDirectivesfalseAllow strings at the start of a file or a function when ecmaVersion is 3, which has no directives.

With an ESLint config file the defaults are ESLint's: every option is false.

By default, you can use && or ? : instead of an if:

example.jsNo problems
isReady && start();
isReady ? start() : wait();

The right side still has to do something:

example.js2 problems
isReady && start;
isReady ? start : wait;
example.jswith { "allowShortCircuit": false }1 problem
isReady && start();
example.jswith { "allowTernary": false }1 problem
isReady ? start() : wait();

A tag is a function, so a tagged template can have an effect:

example.jswith { "allowTaggedTemplates": true }No problems
sql`DELETE FROM sessions`;
example.jsxwith { "enforceForJSX": true }1 problem
<Spinner />;