Skip to content

useUnaryMinus (JavaScript)

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

Enforce the use of the unary minus operator over multiplying or dividing by -1.

Multiplying or dividing a value by -1 is an indirect way to negate it (flip its sign). The unary minus operator (-x) is shorter and states the intent directly.

The rule reports x * -1, -1 * x, and x / -1. It doesn’t report -1 / x, because dividing -1 by x doesn’t produce -x.

The fix is unsafe because it can change behavior for BigInt values: x * -1 throws a TypeError when x is a BigInt, but -x doesn’t. Biome doesn’t offer a fix when the expression contains comments that the fix would remove.

const inverted = value * -1;
code-block.js:1:18 lint/nursery/useUnaryMinus  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ Multiplying by -1 is a roundabout way to negate a value.

> 1 │ const inverted = value * -1;
│ ^^^^^^^^^^
2 │

ℹ The unary minus operator is shorter and states the intent directly.

ℹ 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.

ℹ Unsafe fix: Use the unary minus operator.

1 │ - const·inverted·=·value·*·-1;
1 │ + const·inverted·=·-value;
2 2 │

const flipped = -1 * someNumber;
code-block.js:1:17 lint/nursery/useUnaryMinus  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ Multiplying by -1 is a roundabout way to negate a value.

> 1 │ const flipped = -1 * someNumber;
│ ^^^^^^^^^^^^^^^
2 │

ℹ The unary minus operator is shorter and states the intent directly.

ℹ 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.

ℹ Unsafe fix: Use the unary minus operator.

1 │ const·flipped·=·-1·*·someNumber;
│ ----
const divided = value / -1;
code-block.js:1:17 lint/nursery/useUnaryMinus  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ Dividing by -1 is a roundabout way to negate a value.

> 1 │ const divided = value / -1;
│ ^^^^^^^^^^
2 │

ℹ The unary minus operator is shorter and states the intent directly.

ℹ 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.

ℹ Unsafe fix: Use the unary minus operator.

1 │ - const·divided·=·value·/·-1;
1 │ + const·divided·=·-value;
2 2 │

const inverted = -value;
const reciprocal = -1 / value;
const doubled = value * -2;