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 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.
x &= y
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)

let a = 14; 
let b = 9;  

a &= b; 

console.log(a); // Output: 8
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:
  ...0000 1110  (14)
& ...0000 1001  (9)

  ...0000 1000  (8)
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:
let bigX = 0b111100001111000011110000111100001111n;
let bigY = 0b101010101010101010101010101010101010n;

bigX &= bigY;

console.log(bigX); // Output: 43118103050n (0b101000001010000010100000101000001010n)
Attempting to mix BigInt and Number results in a TypeError:
let mixed = 10n;
mixed &= 5; // TypeError: Cannot mix BigInt and other types, use explicit conversions

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:
let x = 5.99; // Truncated to 5 (...0101)
x &= 3;       // 3 is (...0011)
// 5 & 3 evaluates to 1 (...0001)
console.log(x); // Output: 1

let y = "12"; // Coerced to number 12 (...1100)
y &= 10;      // 10 is (...1010)
// 12 & 10 evaluates to 8 (...1000)
console.log(y); // Output: 8

let z = NaN;  // Coerced to 0
z &= 5;       
console.log(z); // Output: 0
Master JavaScript with Deep Grasping Methodology!Learn More