> For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt.

# no-unneeded-ternary

[Added in v0.5.0](https://github.com/web-infra-dev/rslint/releases/tag/v0.5.0)
## Configuration


```ts title=rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-unneeded-ternary': 'error',
    },
  },
]);
```

## Rule Details

Disallows ternary expressions when a simpler alternative exists. Two patterns
are flagged:

- **Boolean-literal selection** — `cond ? true : false` (or any combination of
  boolean literals on both arms) collapses to `cond`, `!cond`, `!!cond`, or
  the boolean literal itself. Always reported.
- **Default assignment** — `a ? a : b` is equivalent to `a || b`. Reported
  only when the `defaultAssignment` option is set to `false`.

Examples of **incorrect** code for this rule:

```javascript
var a = x === 2 ? true : false;
var b = x ? true : false;
```

Examples of **correct** code for this rule:

```javascript
var a = x === 2 ? "Yes" : "No";
var b = x !== false;
var c = x ? "Yes" : "No";
var d = x ? y : x;
```

Examples of **incorrect** code for this rule with `{ "defaultAssignment": false }`:

```json
{ "no-unneeded-ternary": ["error", { "defaultAssignment": false }] }
```

```javascript
var a = x ? x : 1;
f(x ? x : 1);
```

## Options

| Option              | Type    | Default | Description                                                                                    |
| ------------------- | ------- | ------- | ---------------------------------------------------------------------------------------------- |
| `defaultAssignment` | boolean | `true`  | When `false`, also flag the `a ? a : b` default-assignment pattern (auto-fixed to `a \|\| b`). |

## Original Documentation

- [ESLint: no-unneeded-ternary](https://eslint.org/docs/latest/rules/no-unneeded-ternary)
- [Source code](https://github.com/eslint/eslint/blob/v10.8.1/lib/rules/no-unneeded-ternary.js)
