Skip to content

noXorAsExponentiation (JavaScript)

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

Disallow the bitwise XOR operator where exponentiation was likely intended.

In JavaScript, ^ is the bitwise XOR operator, not exponentiation. Developers coming from languages like Lua, Julia, R, or MATLAB, or from math notation, often expect ^ to mean “to the power of”, so 2 ^ 32 silently evaluates to 34 instead of 4294967296. The actual exponentiation operator is **.

This rule flags ^ between two decimal integer literals, which is almost always this mistake. Hexadecimal, octal, and binary literals (such as 0xFF ^ 8) and any non-literal operands (such as flags ^ MASK) are ignored, since those are far more likely to be intentional bitwise XOR.

const kibibyte = 2 ^ 10; // 8, not 1024
code-block.js:1:20 lint/nursery/noXorAsExponentiation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This bitwise XOR operator ^ is used between two integer literals.

> 1 │ const kibibyte = 2 ^ 10; // 8, not 1024
^
2 │

In JavaScript, ^ is the bitwise XOR operator, not exponentiation. The exponentiation operator is **.

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 - const·kibibyte·=·2·^·10;·//·8,·not·1024
1+ const·kibibyte·=·2·**·10;·//·8,·not·1024
2 2

const cube = 3 ^ 3; // 0, not 27
code-block.js:1:16 lint/nursery/noXorAsExponentiation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This bitwise XOR operator ^ is used between two integer literals.

> 1 │ const cube = 3 ^ 3; // 0, not 27
^
2 │

In JavaScript, ^ is the bitwise XOR operator, not exponentiation. The exponentiation operator is **.

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 - const·cube·=·3·^·3;·//·0,·not·27
1+ const·cube·=·3·**·3;·//·0,·not·27
2 2

const kibibyte = 2 ** 10;
const cube = 3 ** 3;
const masked = flags ^ MASK;
const bits = 0xFF ^ 8;