Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Bitwise OR assignment (|=)

The |= operator performs bitwise OR on the target's current value and the right operand, then performs an assignment of the computed result to the target. It can set the bits selected by a mask without clearing the others and supports Number and BigInt.

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 |= expression

target |= expression computes the bitwise OR (|) of the target's current value and the right operand, then performs an assignment of the result to the target. If the assignment completes normally, the expression evaluates to the computed OR result. A property setter can return normally without storing the value it receives; the expression still evaluates to the computed OR result. Each result bit is 1 if at least one corresponding operand bit is 1. A typical use is enabling flags with a mask.

For a simple variable, flags |= mask behaves like flags = flags | mask, but the expression identifying the target is evaluated only once. This matters for property access with side effects. In non-strict code, |= on a non-writable data property can complete normally: the property remains unchanged, and the expression evaluates to the computed OR result. In strict mode, such a write throws a TypeError. Attempting to modify a variable declared with const throws a TypeError in either mode.

Operands undergo numeric conversion. For Number, the operation converts them to signed 32-bit integers: it discards fractional parts and retains the low 32 bits. The result is also a signed 32-bit integer, so it can be negative. For BigInt, there is no 32-bit limit. After conversion, both operands must have the same numeric type; combining Number and BigInt throws a TypeError.

Do not confuse |= with logical OR assignment (||=). |= does not short-circuit based on the left value's truthiness; during normal evaluation it evaluates the right operand and performs the assignment. In the example, 0b0001 | 0b0100 produces 0b0101, or 5. Applying the same mask again leaves those bits set.

Examples

let flags = 0b0001;
const mask = 0b0100;
const result = (flags |= mask);
console.log(flags);
console.log(result);
flags |= mask;
console.log(flags);

Result

5
5
5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources