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 TypeScript is the bitwise XOR (exclusive OR) operator. It evaluates two numeric operands by performing a logical exclusive OR operation on each corresponding pair of bits. For standard number operands, this involves converting them to 32-bit signed integers, whereas bigint operands operate at arbitrary precision. The operator evaluates bits according to the following truth table:
  • 0 ^ 0 yields 0
  • 1 ^ 1 yields 0
  • 0 ^ 1 yields 1
  • 1 ^ 0 yields 1
const operandA: number = 0b1010;
const operandB: number = 0b1100;
const result: number = operandA ^ operandB; // 0b0110

Evaluation Mechanics

When the TypeScript compiler transpiles and the JavaScript engine executes the ^ operator, the execution behavior depends strictly on the type of the operands: For number and numeric enum operands:
  1. Type Coercion and Truncation: Both operands are converted to 32-bit signed integers using two’s complement representation. Any fractional components are discarded (e.g., 5.9 becomes 5).
  2. Bitwise Comparison: The engine aligns the 32 bits of both operands and compares them positionally.
  3. Return Value: A new 32-bit signed integer is returned based on the XOR evaluation of the bit pairs.
const a: number = 5;  // Binary: ...0000 0101
const b: number = 3;  // Binary: ...0000 0011

const result: number = a ^ b; 

// Bitwise Evaluation:
//   0101 (5 in decimal)
// ^ 0011 (3 in decimal)
// ---
//   0110 (6 in decimal)

console.log(result); // Outputs: 6
For bigint operands: The operands are not truncated to 32 bits. The engine aligns the binary representation of the arbitrary-precision integers and performs the XOR operation across all bits, returning a new bigint.

TypeScript Type Constraints

TypeScript enforces strict type checking on the ^ operator to prevent unintended runtime coercion: Number and Enum Operands: Operands can be of type number, any, or a numeric enum.
enum Flags {
  Read = 1,
  Write = 2
}

const val1: number = 10;
const val2: Flags = Flags.Write;
const out: number = val1 ^ val2; // Valid, returns 8
BigInt Operands: The ^ operator fully supports bigint types. Because bigint operates at arbitrary precision, no 32-bit truncation occurs.
const big1: bigint = 10n;
const big2: bigint = 4n;
const bigOut: bigint = big1 ^ big2; // Valid, returns 14n
Mixed Types (Compilation Error): TypeScript will throw a ts(2365) error if you attempt to mix number (or enum) and bigint operands, as the underlying JavaScript engine cannot implicitly mix these types for bitwise operations.
const num: number = 5;
const big: bigint = 3n;

// Error: Operator '^' cannot be applied to types 'number' and 'bigint'.
const invalid = num ^ big; 

Compound Assignment

TypeScript supports the compound assignment operator ^=, which applies the bitwise XOR operation and assigns the resulting value back to the left operand.
let x: number = 5;
x ^= 3; // Syntactic sugar for: x = x ^ 3;
// x is now 6
Master TypeScript with Deep Grasping Methodology!Learn More