# no-constant-condition

> Disallow conditions that are always truthy or always falsy

If a condition never changes, one branch always runs and the other is dead code. It's often a mistake. For example, `role === "admin" || "owner"` is always truthy, because `"owner"` is a non-empty string.

The rule checks the test of `if`, `? :`, `while`, `do...while` and `for`. `while (true)` is allowed by default.

This rule is on by default.

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

## Configure

On by default as a warning. To turn it off:

```toml bunfig.toml
[lint.rules]
"no-constant-condition" = "off" # or "error"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"no-constant-condition" = ["warn", {
  # "all", "allExceptWhileTrue", "none", true or false
  checkLoops = "allExceptWhileTrue",
}]
```

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

## Examples

Reported:

{/* bad {"count": 2} */}
```js
if (role === "admin" || "owner") {
  showSettings();
}

const title = "" ? "Draft" : "Published";
```

Not reported:

{/* good */}
```js
if (role === "admin" || role === "owner") {
  showSettings();
}

while (true) {
  const job = queue.shift();
  if (!job) break;
  run(job);
}
```

## Options

`checkLoops` controls how loops are checked:

| Value | What it does |
| --- | --- |
| `"allExceptWhileTrue"` (default) | Check loops, but allow `while (true)`. |
| `"all"` or `true` | Check loops, including `while (true)`. |
| `"none"` or `false` | Don't check loops. |

{/* bad {"options": [{"checkLoops": "all"}]} */}
```js
while (true) {
  poll();
}
```

The default only allows the literal `true` in a `while`:

{/* bad {"count": 2} */}
```js
while (1) {
  poll();
}

do {
  poll();
} while (true);
```

{/* good {"options": [{"checkLoops": "none"}]} */}
```js
while (1) {
  poll();
}
```

A loop in a generator function isn't reported if it contains a `yield`, because the caller decides when to stop:

{/* good {"options": [{"checkLoops": "all"}]} */}
```js
function* ids() {
  let next = 0;
  while (true) {
    yield next++;
  }
}
```
