CKTIS / JavaScript
Bitwise XOR assignment (^=)
The ^= operator computes the bitwise exclusive OR of the target’s current value and the right operand, then assigns the result to that target. It can toggle bits using a mask and supports Number and BigInt, but does not allow mixing those types.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
target ^= expressiontarget ^= expression performs bitwise XOR and assigns the result to target. Each result bit is 1 when the corresponding operand bits differ and 0 when they match. A mask therefore toggles bits marked with 1 and leaves the others unchanged. This is neither exponentiation nor a logical operator working on truth values.
For a simple variable, x ^= y produces the same result as x = x ^ y, but evaluates the left-hand side only once. This matters for property access with side effects. The target’s current value is read before the right-hand side is evaluated. The assignment expression returns the computed XOR value, even if a property setter accepts the write without storing the value. This can also happen without a setter: in non-strict code, a write to a non-writable data property is ignored, but the expression still returns the XOR result. In strict mode, that write throws a TypeError.
Operands undergo conversion to numeric values. For Number, XOR works on signed 32-bit integers: fractional parts are discarded, and out-of-range values wrap to 32 bits. For BigInt, there is no 32-bit limit. After conversion, both operands must have the same numeric type; combining Number and BigInt throws TypeError.
In the example, 0b1010 XOR 0b0110 produces 0b1100, or 12. Applying the same mask again restores 10. The BigInt portion produces the same numerical result, while String(wide) makes the output text unambiguous.
Examples
let flags = 0b1010;
const mask = 0b0110;
console.log(flags ^= mask);
console.log(flags ^= mask);
let wide = 10n;
wide ^= 6n;
console.log(String(wide));Result
12 10 12
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.