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 | (Bitwise OR) operator performs a logical OR operation on each pair of corresponding bits of its operands. Depending on the operand types, it evaluates them either as 32-bit signed integers (for Number values) or as arbitrary-precision integers (for BigInt values), returning a new value of the matching type where each bit is set to 1 if at least one of the corresponding bits in the operands is 1.

Syntax

operand1 | operand2

Execution Mechanics

  1. Type Evaluation and Coercion:
    • Number Operands: If the operands are standard numbers (or non-BigInt values coercible to numbers), JavaScript passes them through the internal ToInt32 abstract operation.
      • Fractional values are strictly truncated (the decimal portion is discarded).
      • Non-numeric values are coerced to numbers. Values that resolve to NaN, null, or undefined are converted to 0.
      • Numeric values exceeding the 32-bit signed integer limit wrap around.
    • BigInt Operands: If both operands are BigInt values, they are evaluated as arbitrary-precision integers using two’s complement representation. They bypass ToInt32 entirely and are not truncated to 32 bits.
    • Mixed Types: Attempting to apply the operator to a mix of BigInt and Number operands throws a TypeError.
  2. Bitwise Alignment: The JavaScript engine aligns the binary representations of both integers. For Number operands, this is strictly bounded to 32 bits. For BigInt operands, the representation conceptually extends with infinite sign bits to accommodate the magnitude of the values.
  3. Evaluation: A logical OR is applied to each bit position according to the following truth table:
    • 0 | 0 yields 0
    • 0 | 1 yields 1
    • 1 | 0 yields 1
    • 1 | 1 yields 1

Code Visualization

Standard Number Evaluation
const a = 5;  // Binary: 00000000000000000000000000000101
const b = 3;  // Binary: 00000000000000000000000000000011

console.log(a | b); // Output: 7
// Result Binary:      00000000000000000000000000000111
BigInt Evaluation
const bigA = 8589934592n; // 2^33 (Exceeds 32-bit limit)
const bigB = 5n;

// Evaluates with arbitrary precision, no 32-bit truncation
console.log(bigA | bigB); // Output: 8589934597n
Mixed Type Error
console.log(5n | 3); 
// Output: TypeError: Cannot mix BigInt and other types, use explicit conversions
Type Coercion and Truncation (Number only)
// Floating-point truncation
console.log(5.99 | 1.2); // Output: 5
// Step 1: 5.99 is truncated to 5 (0101)
// Step 2: 1.2 is truncated to 1 (0001)
// Step 3: 5 | 1 evaluates to 5 (0101)

// String coercion
console.log("10" | 2); // Output: 10
// Step 1: "10" is coerced to integer 10 (1010)
// Step 2: 2 remains 2 (0010)
// Step 3: 10 | 2 evaluates to 10 (1010)

// NaN / Undefined coercion
console.log(NaN | 4); // Output: 4
// Step 1: NaN is coerced to 0 (0000)
// Step 2: 4 remains 4 (0100)
// Step 3: 0 | 4 evaluates to 4 (0100)
Negative Integer Evaluation (Two’s Complement)
const c = -5; // Binary: 11111111111111111111111111111011
const d = 2;  // Binary: 00000000000000000000000000000010

console.log(c | d); // Output: -5
// Result Binary:      11111111111111111111111111111011
Master JavaScript with Deep Grasping Methodology!Learn More