Skip to content

useStrictBooleanExpressions (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useStrictBooleanExpressions": "error"
}
}
}
}

Require unambiguous boolean expressions in conditions.

Truthiness checks on nullable primitives can confuse missing values with false, an empty string, zero, or NaN. Check for nullish values explicitly or convert the value with Boolean() when that distinction is intentional.

This rule allows booleans, non-nullable strings and numbers, and nullable objects, functions, and symbols. Nullable true, nonempty string literal types, and nonzero number literal types are also allowed. Numbers include bigints, but nullable bigints are rejected.

It checks conditions, logical negation, the operands of && and ||, and array predicate callbacks and truthiness assertion arguments. The last operand of a logical expression is checked only when its result is used as a condition.

Types that cannot be inferred are ignored.

nullable-number.ts
function display(count: number | undefined) {
if (count) {}
}
/nullable-number.ts:2:9 lint/nursery/useStrictBooleanExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This condition uses a nullable number.

1 │ function display(count: number | undefined) {
> 2 │ if (count) {}
│ ^^^^^
3 │ }
4 │

ℹ This check treats zero, NaN, and nullish values alike.

ℹ Check for nullish values and falsy numbers explicitly, or use Boolean() to test truthiness.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

nullable-boolean.ts
function run(enabled?: boolean) {
if (enabled) {}
}
/nullable-boolean.ts:2:9 lint/nursery/useStrictBooleanExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This condition uses a nullable boolean.

1 │ function run(enabled?: boolean) {
> 2 │ if (enabled) {}
│ ^^^^^^^
3 │ }
4 │

ℹ This check treats false and nullish values alike.

ℹ Compare with true or provide an explicit default with ?? false.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

object-condition.js
if ({}) {}
/object-condition.js:1:5 lint/nursery/useStrictBooleanExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This condition uses an always-truthy value.

> 1 │ if ({}) {}
│ ^^
2 │

ℹ Objects, functions, and symbols are always truthy.

ℹ Check a property of the value or remove the condition.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

function run(maybeCount: number | undefined, count: number, text: string, object: object | null, enabled?: boolean) {
if (maybeCount != null) {}
if (enabled === true) {}
if (text) {}
if (count) {}
if (object) {}
}