# unicorn/prefer-ternary

> Require a ternary instead of an `if`/`else` where both branches do the same kind of thing

When both branches of an `if`/`else` only return, throw, `await`, `yield`, or assign to the same variable, the statement repeats itself and the only difference is the value. A ternary writes the common part once.

The rule skips an `if` when it's an `else if`, when a branch has more than one statement, or when the condition or one of the values is already a ternary.

{/* 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]
"unicorn/prefer-ternary" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
# "always" or "only-single-line"
"unicorn/prefer-ternary" = ["error", "always"]
```

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

## Examples

Reported:

{/* bad {"count": 2} */}
```js
function label(user) {
  if (user.isAdmin) {
    return "admin";
  } else {
    return "member";
  }
}

let timeout;
if (retrying) {
  timeout = 5000;
} else {
  timeout = 1000;
}
```

Not reported:

{/* good */}
```js
function label(user) {
  return user.isAdmin ? "admin" : "member";
}

const timeout = retrying ? 5000 : 1000;

if (retrying) {
  attempts = 0;
} else {
  failures = 0;
}
```

## Options

| Option | What it does |
| --- | --- |
| `"always"` (default) | Report every `if`/`else` that can be a ternary. |
| `"only-single-line"` | Only report when the condition and the statement in each branch fit on one line each. |

{/* good {"options": ["only-single-line"]} */}
```js
if (cached) {
  result = cache.get(key);
} else {
  result = load({
    key,
    retries: 3,
  });
}
```

{/* bad {"options": ["only-single-line"]} */}
```js
if (cached) {
  result = cache.get(key);
} else {
  result = load(key);
}
```
