docs/src/rules/no-bitwise.md
The use of bitwise operators in JavaScript is very rare and often & or | is simply a mistyped && or ||, which will lead to unexpected behavior.
const x = y | z;
This rule disallows bitwise operators.
Examples of incorrect code for this rule:
::: incorrect
/*eslint no-bitwise: "error"*/
let x = y | z;
const x1 = y & z;
const x2 = y ^ z;
const x3 = ~ z;
const x4 = y << z;
const x5 = y >> z;
const x6 = y >>> z;
x |= y;
x &= y;
x ^= y;
x <<= y;
x >>= y;
x >>>= y;
:::
Examples of correct code for this rule:
::: correct
/*eslint no-bitwise: "error"*/
let x = y || z;
const x1 = y && z;
const x2 = y > z;
const x3 = y < z;
x += y;
:::
This rule has an object option:
"allow": Allows a list of bitwise operators to be used as exceptions."int32Hint": Allows the use of bitwise OR in |0 pattern for type casting.Examples of correct code for this rule with the { "allow": ["~"] } option:
::: correct
/*eslint no-bitwise: ["error", { "allow": ["~"] }] */
~[1,2,3].indexOf(1) === -1;
:::
Examples of correct code for this rule with the { "int32Hint": true } option:
::: correct
/*eslint no-bitwise: ["error", { "int32Hint": true }] */
const b = a|0;
:::