CKTIS / JavaScript
Bitwise AND assignment (&=)
The &= operator computes the bitwise AND of the target’s current value and the right operand, then stores the result in that same target. It is useful for retaining only bits selected by a mask. It supports Number and BigInt, but not a mixture of them.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
target &= expressiontarget &= expression keeps a bit set to 1 only when both corresponding bits are 1. It stores the result in target; the assignment expression itself also evaluates to that result. For a simple variable, x &= mask produces the same result as x = x & mask.
An important difference concerns target evaluation: in object[key()] &= mask, the left-hand reference is evaluated only once. Its previous value is read first, and then the right-hand side is evaluated. This is not logical assignment &&=: &= does not short-circuit or skip the right-hand side merely because the left-hand value is 0.
Operands are converted to numeric values. For Number, the operation uses signed 32-bit integers: fractional parts are discarded, and bits beyond 32 bits are lost. For BigInt, that width restriction does not apply. Mixing Number and BigInt after conversion throws TypeError. The target must allow writing; assigning to a variable declared with const throws TypeError.
In the example, the mask retains only bits from 0b1101 that also occur in 0b1010, producing 0b1000, or 8. Both flags and result hold 8; toString(2) displays the binary representation without a prefix.
Examples
let flags = 0b1101;
const mask = 0b1010;
const result = (flags &= mask);
console.log(flags);
console.log(result);
console.log(flags.toString(2));Result
8 8 1000
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.