&= (bitwise AND assignment) operator performs a bitwise AND operation between the binary representations of its left and right operands, assigning the computed 32-bit integer result back to the left operand.
Mechanical Behavior
- Type Coercion (
ToInt32): Before the operation occurs, the TypeScript/JavaScript runtime implicitly coerces both operands into 32-bit signed integers. Fractional values are truncated. - Bitwise Evaluation: The operator compares the operands bit by bit. A bit in the resulting integer 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 32-bit integer is assigned to the memory location of the left operand.
Truth Table
| Bit A (Left) | Bit B (Right) | Result (A & B) |
|---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Execution Example
TypeScript Compiler Constraints
While the underlying JavaScript engine will attempt to coerce any type into a number for bitwise operations, the TypeScript compiler enforces strict type safety:- Operand Types: TypeScript requires both the left and right operands to be of type
number(orany). Attempting to use&=with strings, booleans, or objects will throw a compilation error (e.g.,TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.). - Mutability: The left operand must be a mutable reference. It cannot be a constant (
const) or a read-only property. - BigInt Support: The
&=operator also supportsbiginttypes, provided both operands arebigint. Mixingnumberandbigintwill result in aTS2365compiler error. When operating onbigint, the operation is not restricted to 32 bits.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More





