The bitwise OR (|) operator in JavaScript is used to perform a bitwise OR operation on the individual bits of two numbers. It returns a number where each bit is set (1) if at least one of the corresponding bits of the two numbers being ORed is 1.
Here's how the bitwise OR operator works with a simple example:
let num1 = 5; // Binary representation: 00000101
let num2 = 3; // Binary representation: 00000011
let result = num1 | num2;
In this example, the OR operation is performed on num1 and
num2. The result has a 1 at any position where at least one of the input numbers has a 1. So,
5 | 3 results in 7 because the bits in the binary representation differ at the 2^0, 2^1, and 2^2 positions.
The bitwise OR operator is often used in situations where you want to set specific bits in a binary representation to 1, combining different sets of flags, or performing other bit-level manipulations in your code.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The bitwise OR (|) operator in JavaScript is used to perform a bitwise OR operation on the individual bits of two numbers. It returns a number where each bit is set (1) if at least one of the corresponding bits of the two numbers being ORed is 1.
Here's how the bitwise OR operator works with a simple example:
In this example, the OR operation is performed on num1 and num2. The result has a 1 at any position where at least one of the input numbers has a 1. So, 5 | 3 results in 7 because the bits in the binary representation differ at the 2^0, 2^1, and 2^2 positions.
The bitwise OR operator is often used in situations where you want to set specific bits in a binary representation to 1, combining different sets of flags, or performing other bit-level manipulations in your code.