Skip to content

noAccidentalBitwiseOperators (JavaScript)

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

Disallow bitwise operators where a logical operator was likely intended.

The bitwise operators &, |, and |= look a lot like the logical operators &&, ||, and ||=, so it is easy to type one when you meant the other. The two behave very differently:

  • Bitwise operators always evaluate both sides. In obj & obj.prop, obj.prop is read even when obj is null or undefined, which throws an error.
  • Bitwise operators turn both sides into whole numbers. In options | {}, the object becomes 0, so the result is a number instead of options or {}.

This rule only reports cases where a logical operator was clearly intended:

  • & between a variable and a property of that same variable, such as obj & obj.prop.
  • | or |= with a right side that can never be a useful number: an object, an array, a class, a function, a string, a template string, or true/false.

Bitwise math such as flags & MASK or value | 0 is not reported.

if (obj & obj.prop) {}
code-block.js:1:9 lint/nursery/noAccidentalBitwiseOperators  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The bitwise operator & is used where the logical operator && was likely intended.

> 1 │ if (obj & obj.prop) {}
│ ^
2 │

ℹ Bitwise operators always evaluate both sides and turn them into numbers, so the result is not the value of either side.

ℹ 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: Replace & with &&.

1 │ if·(obj·&&·obj.prop)·{}
│ +
options = options | {};
code-block.js:1:19 lint/nursery/noAccidentalBitwiseOperators  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The bitwise operator | is used where the logical operator || was likely intended.

> 1 │ options = options | {};
│ ^
2 │

ℹ Bitwise operators always evaluate both sides and turn them into numbers, so the result is not the value of either side.

ℹ 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: Replace | with ||.

1 │ options·=·options·||·{};
│ +
input |= "";
code-block.js:1:7 lint/nursery/noAccidentalBitwiseOperators  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The bitwise operator |= is used where the logical operator ||= was likely intended.

> 1 │ input |= "";
│ ^^
2 │

ℹ Bitwise operators always evaluate both sides and turn them into numbers, so the result is not the value of either side.

ℹ 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: Replace |= with ||=.

1 │ input·||=·"";
│ +
if (obj && obj.prop) {}
options = options || {};
input ||= "";
const masked = flags & MASK;
const truncated = value | 0;