Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Bitwise OR operator (|)

The | operator combines the bits of two values: a result bit is 1 when at least one corresponding operand bit is 1. It supports Number and BigInt, but does not allow mixing those types.

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

left | right

The left | right operator performs inclusive bitwise OR: 0 | 0 produces 0, and every other pair of bits produces 1. It is useful for combining flags assigned to separate bits. In the example, READ | WRITE combines 001 and 010, producing 011, or 3.

Operands are converted to numeric values. For Number, both are then converted to signed 32-bit integers; the result still has type Number. The fractional part is discarded toward zero, and the value wraps modulo 2 ** 32 and is interpreted as signed. Consequently, 2147483648 | 0 produces -2147483648. The values NaN, Infinity, and -Infinity convert to zero. The expression x | 0 is not a safe way to truncate arbitrary numbers.

For two BigInt values, the result is a BigInt, without a 32-bit limit. Negative numbers behave like two's-complement representations with infinite sign extension. If one operand is a Number and the other a BigInt after conversion, a TypeError is thrown; 1n | 1 is an example.

Do not confuse | with logical ||: | does not short-circuit and produces a numeric result, whereas || can skip the right operand and returns one of its operands. The | operator has lower precedence than &, ^, and equality comparisons; use parentheses, for example (permissions | READ) === permissions.

Examples

const READ = 0b001;
const WRITE = 0b010;
const permissions = READ | WRITE;
console.log(permissions);
console.log(permissions.toString(2));
console.log(2147483648 | 0);
console.log(String(5n | 3n));

Result

3
11
-2147483648
7

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