π Disallow bitwise operators where a logical operator was likely intended.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π‘ This rule is manually fixable by editor suggestions.
Bitwise operators (&, |, |=) are rarely used in everyday code, and when they show up in boolean-like contexts they are almost always a typo for the logical operators (&&, ||, ||=).
The bug is easy to miss and hard to debug:
- Bitwise operators do not short-circuit, so
if (object & object.property)evaluatesobject.propertyeven whenobjectisnullorundefined, throwing aTypeError. options | {}coerces both sides to integers, silently producingoptions | 0instead of falling back to the default object.
This rule only targets the high-signal patterns where a logical operator was clearly intended, so it does not flag legitimate bitwise math like flags & MASK or value | 0. Because switching the operator changes runtime behavior, the fix is offered as a suggestion rather than applied automatically.
This is more targeted than the built-in no-bitwise rule, which disallows all bitwise operators. See also prefer-math-trunc, which covers the value | 0 truncation idiom.
// β
if (object & object.property) {
// β¦
}
// β
if (object && object.property) {
// β¦
}// β
options = options | {};
// β
options = options || {};// β
input |= '';
// β
input ||= '';// β
Not flagged: legitimate bitwise math
const masked = flags & MASK;
const truncated = value | 0;