Favorites

JavaScript ^ Bitwise XOR Operator

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
^
JavaScript Bitwise OperatorThis operator is supported by all modern browsers.
Updated 28th November 2026

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 ^ y

Example

console.log(5 ^ 3);
let flags = 0b0101;
flags ^= 0b0001;
console.log(flags.toString(2));
console.log(2 ^ 3, 2 ** 3);

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.

This operator is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee