TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
^ 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.
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:
- Type Coercion: Non-numeric types are implicitly converted to numbers. Values that cannot be converted to valid numbers (like
NaNorundefined) are coerced to0. - Truncation: Any fractional components of floating-point numbers are discarded.
- 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 A | Bit B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Evaluation Examples
Standard Number Evaluation Consider the expression5 ^ 3:
- The integer
5is represented in 32-bit binary as:00000000000000000000000000000101 - The integer
3is represented in 32-bit binary as:00000000000000000000000000000011 - The XOR operation compares them vertically:
00000000000000000000000000000110 - The resulting binary translates back to the base-10 integer
6.
BigInt values, but without the 32-bit boundary constraints.
Type Coercion Behavior
Because of theToInt32 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.
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.
Master JavaScript with Deep Grasping Methodology!Learn More





