Skip to main content
The |= (Bitwise OR assignment) operator evaluates the binary representations of its left and right operands, performs a bitwise OR operation (|), and assigns the computed result to the left operand.
While functionally similar to x = x | y, the |= operator evaluates the left-hand expression only once. This semantic distinction is critical when the left operand contains side effects, such as property accessors or increment/decrement operations.

Execution Mechanics

When the JavaScript engine encounters the |= operator, it executes the following sequence based on the operand types:
  1. Type Resolution: The engine determines the numeric type of the operands. If one operand is a BigInt and the other is a Number, the engine throws a TypeError.
  2. Numeric Conversion and Alignment:
    • For Number operands: Both operands are implicitly converted to 32-bit signed integers using the internal ToInt32 abstract operation. Fractional values are truncated (e.g., 5.9 becomes 5), and non-numeric values are coerced to numbers (or 0 if coercion results in NaN). The engine aligns their 32-bit two’s complement binary representations.
    • For BigInt operands: The engine does not truncate to 32 bits. It aligns the arbitrary-precision integers using two’s complement representation.
  3. Bitwise Evaluation: A logical OR is applied to each pair of corresponding bits. The resulting bit is 1 if at least one of the corresponding bits in the operands is 1. It is 0 only if both bits are 0.
  4. Assignment: The resulting integer (either a 32-bit Number or a BigInt) is assigned back to the left operand.

Bitwise OR Truth Table

Code Visualization

Coercion and Type Behavior

Because of the mandatory ToInt32 conversion for standard numbers and the strict type requirements for BigInt, using |= yields specific structural behaviors:
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More