Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ^ operator in JavaScript is the Bitwise XOR (Exclusive OR) operator. It evaluates two numeric operands at the binary level, returning a 1 in each bit position where the corresponding bits of the operands differ, and a 0 where they are identical.
operand1 ^ operand2

Execution Mechanics and Coercion

The JavaScript engine evaluates the ^ operator differently depending on whether the operands are standard Number types or BigInt types. Standard Number Operands Before performing the bitwise operation on standard numbers, the engine applies the ToInt32 abstract operation to both operands. This enforces the following transformations:
  1. Type Coercion: Non-numeric types are implicitly converted to numbers. Values that cannot be converted to valid numbers (like NaN or undefined) are coerced to 0.
  2. Truncation: Any fractional components of floating-point numbers are discarded.
  3. 32-bit Conversion: The resulting integers are treated as 32-bit signed integers using two’s complement representation.
BigInt Operands When both operands are BigInt values, the ^ operator evaluates them as arbitrarily large integers. BigInt operands do not undergo the ToInt32 abstract operation and are never truncated to 32 bits. The XOR operation is applied across the full binary representation of the arbitrarily large values. Mixed Operands JavaScript does not allow implicit coercion between BigInt and Number types for bitwise operations. Mixing them throws a TypeError.

The XOR Truth Table

Regardless of the numeric type, the operator compares the binary representations bit-by-bit according to the following logic:
Bit ABit BA ^ B
000
011
101
110

Evaluation Examples

Standard Number Evaluation Consider the expression 5 ^ 3:
  1. The integer 5 is represented in 32-bit binary as: 00000000000000000000000000000101
  2. The integer 3 is represented in 32-bit binary as: 00000000000000000000000000000011
  3. The XOR operation compares them vertically: 00000000000000000000000000000110
  4. The resulting binary translates back to the base-10 integer 6.
const a = 5;
const b = 3;

console.log(a ^ b); // Output: 6
BigInt Evaluation The operation works identically on BigInt values, but without the 32-bit boundary constraints.
const largeA = 5n;
const largeB = 3n;

console.log(largeA ^ largeB); // Output: 6n

Type Coercion Behavior

Because of the ToInt32 conversion step for standard numbers, the ^ operator exhibits specific behaviors when encountering non-integer or non-numeric types. Attempting to mix BigInt with other types results in an error.
// Floating-point truncation
5.9 ^ 3.2;       // Output: 6 (Evaluated as 5 ^ 3)

// String coercion
"5" ^ "3";       // Output: 6 (Strings parsed to integers)
"hello" ^ 5;     // Output: 5 (NaN ^ 5 evaluates to 0 ^ 5)

// Boolean coercion
true ^ false;    // Output: 1 (Evaluated as 1 ^ 0)

// Null and Undefined
null ^ 5;        // Output: 5 (Evaluated as 0 ^ 5)
undefined ^ 5;   // Output: 5 (Evaluated as 0 ^ 5)

// Mixed Types (BigInt and Number)
5n ^ 3;          // Throws TypeError: Cannot mix BigInt and other types

Compound Assignment

The Bitwise XOR operator can be combined with the assignment operator (=) to apply the operation and assign the result to the left operand in a single step. This works for both Number and BigInt types.
let x = 5;
x ^= 3; 
console.log(x); // Output: 6

let y = 5n;
y ^= 3n;
console.log(y); // Output: 6n
Master JavaScript with Deep Grasping Methodology!Learn More