The bitwise AND assignment (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.
&=) 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.
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:
- Type Evaluation and Coercion:
- If both operands are of type
Number(or can be coerced toNumber), they are implicitly converted to 32-bit signed integers using two’s complement representation via the internalToInt32abstract operation. Fractional values are truncated, and non-numeric values (e.g.,null,undefined,NaN) are coerced to0. - 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 theBigIntvalues. - If one operand is a
BigIntand the other is aNumber, the engine throws aTypeError(e.g.,Cannot mix BigInt and other types).
- If both operands are of type
- Bitwise Evaluation: The engine compares the operands bit by bit. A bit in the resulting binary sequence is set to
1if and only if the corresponding bits in both operands are1. If either bit is0, the resulting bit is0. - Assignment: The newly calculated value (
NumberorBigInt) is assigned back to the memory space of the left operand.
Step-by-Step Evaluation Example (Number)
a(14) is represented in 32-bit binary as:...0000 1110b(9) is represented in 32-bit binary as:...0000 1001
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:
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:
Master JavaScript with Deep Grasping Methodology!Learn More





