noXorAsExponentiation (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noXorAsExponentiation - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
unicorn/no-xor-as-exponentiation
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noXorAsExponentiation": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const kibibyte = 2 ^ 10; // 8, not 1024code-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 27code-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;Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.