Skip to content

eslint/no-bitwise Restriction ​

What it does ​

Disallow bitwise operators

Why is this bad? ​

The use of bitwise operators in JavaScript is very rare and often & or | is simply a mistyped && or ||, which will lead to unexpected behavior.

Examples ​

Examples of incorrect code for this rule:

javascript
var x = y | z;
javascript
var x = y ^ z;
javascript
var x = y >> z;

Examples of correct code for this rule:

javascript
var x = y || z;
javascript
var x = y && z;
javascript
var x = y > z;

Options ​

allow ​

{ "allow": string[] }

Theallow option permits the given list of bitwise operators to be used as exceptions to this rule.

For example { "allow": ["~"] } would allow the use of the bitwise operator ~ without restriction. Such as in the following:

javascript
~[1, 2, 3].indexOf(1) === -1;

int32Hint ​

{ "int32Hint": boolean }

When set to true the int32Hint option allows the use of bitwise OR in |0 pattern for type casting.

For example with { "int32Hint": true } the following is permitted:

javascript
const b = a | 0;

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny no-bitwise
json
{
  "rules": {
    "no-bitwise": "error"
  }
}

References ​

Released under the MIT License.