# 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`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

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

Every option, set to its default:

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

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```js
server.close;
count === 0;
`Hello, ${name}`;
```

Not reported:

{/* good */}
```js
"use strict";

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

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `allowShortCircuit` | `true` | Allow `a && b()`, `a \|\| b()` and `a ?? b()`, if the right side would be allowed as a statement. |
| `allowTernary` | `true` | Allow `a ? b() : c()`, if both branches would be allowed as statements. |
| `allowTaggedTemplates` | `false` | Allow a tagged template as a statement. |
| `enforceForJSX` | `false` | Also report a JSX element as a statement. |
| `ignoreDirectives` | `false` | Allow 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`:

{/* good */}
```js
isReady && start();
isReady ? start() : wait();
```

The right side still has to do something:

{/* bad {"count": 2} */}
```js
isReady && start;
isReady ? start : wait;
```

{/* bad {"options": [{"allowShortCircuit": false}]} */}
```js
isReady && start();
```

{/* bad {"options": [{"allowTernary": false}]} */}
```js
isReady ? start() : wait();
```

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

{/* good {"options": [{"allowTaggedTemplates": true}]} */}
```js
sql`DELETE FROM sessions`;
```

{/* bad {"options": [{"enforceForJSX": true}]} */}
```jsx
<Spinner />;
```
