JavaScript ^ Bitwise XOR Operator
The bitwise XOR operator (^) sets each bit to 1 where exactly one of two integers has a 1. It can toggle flags.
Description
The bitwise XOR (exclusive OR) operator ^ returns a 1 in each bit position where the operands differ. XOR-ing a value with a flag toggles that flag. Note that ^ is not exponentiation in JavaScript; use ** for powers.
A property of XOR is that applying it twice with the same value returns the original: (a ^ b) ^ b === a. That's used for toggling flags, simple checksums, and the classic trick of swapping two integer variables without a temporary one, although destructuring, [a, b] = [b, a], is clearer.
Because ^ means "raise to the power" in some other languages, it's a common mistake to write 2 ^ 3 expecting 8; in JavaScript that gives 1. Operands are converted to 32-bit integers, and BigInts are supported when both sides are BigInts.
Properties
- Operator
^- Category
- Bitwise
- Precedence
- 6 (1 is lowest, 18 is highest)
- Associativity
- left-to-right
Syntax
x ^ yExample
Browser Support
The following information will show you the current browser support for the ^ operator. Hover over a browser icon to see the version that first introduced support for this operator.
Desktop





Tablets & Mobile





