Skip to main content
The bitwise AND assignment (&=) operator performs a bitwise AND operation on two operands and assigns the resulting value to the left operand. For Number operands, it evaluates their 32-bit binary representations, applies a logical AND to each corresponding pair of bits, and mutates the left operand with the final 32-bit integer result. For BigInt operands, it performs the bitwise AND operation across the arbitrary precision of the operands and assigns the resulting BigInt.
While often conceptualized as x = x & y, the compound assignment operator &= evaluates the left-hand side expression only once. This distinction is critical when the left operand contains side effects, such as property accessors (getters/setters) or complex expressions. For example, in arr[i++] &= y, the index i is incremented only once, whereas arr[i++] = arr[i++] & y would evaluate and increment i twice.

Execution Mechanics

When the &= operator is evaluated, the JavaScript engine performs the following sequence of operations depending on the operand types:
  1. Type Evaluation and Coercion:
    • If both operands are of type Number (or can be coerced to Number), they are implicitly converted to 32-bit signed integers using two’s complement representation via the internal ToInt32 abstract operation. Fractional values are truncated, and non-numeric values (e.g., null, undefined, NaN) are coerced to 0.
    • If both operands are of type BigInt, no 32-bit truncation occurs. The operation is performed using the arbitrary-precision two’s complement representation of the BigInt values.
    • If one operand is a BigInt and the other is a Number, the engine throws a TypeError (e.g., Cannot mix BigInt and other types).
  2. Bitwise Evaluation: The engine compares the operands bit by bit. A bit in the resulting binary sequence is set to 1 if and only if the corresponding bits in both operands are 1. If either bit is 0, the resulting bit is 0.
  3. Assignment: The newly calculated value (Number or BigInt) is assigned back to the memory space of the left operand.

Step-by-Step Evaluation Example (Number)

Binary Breakdown:
  • a (14) is represented in 32-bit binary as: ...0000 1110
  • b (9) is represented in 32-bit binary as: ...0000 1001
The bitwise AND operation aligns the bits:
The resulting binary 1000 evaluates to the base-10 integer 8, which is then assigned to a.

BigInt Behavior

When using BigInt operands, the bitwise AND operation scales to the precision of the values without 32-bit truncation:
Attempting to mix BigInt and Number results in a TypeError:

Coercion Behavior (Number Context)

Because the operator forces a ToInt32 conversion for non-BigInt types, applying &= to non-integer types results in strict truncation and type casting before the bitwise operation occurs:
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More