Open In App

OR(|) Bitwise Operator in JavaScript

JavaScript Bitwise OR(|) Operator is used to compare two operands by performing OR operation on individual bits of the operands and returns true even if one of the compared bits is 1. The OR Operator has vast applications and the most used one is combining bit values. The operation is represented by the “|” symbol.

Let’s look at the truth table below to better understand the output of the OR operation between two bits.



A B OUTPUT ( A | B )
0 0 0
0 1 1
1 0 1
1 1 1

Syntax: 

a | b

Example 1: In this example, we will understand the basics of OR operation.






let a=3;
let b=5;
console.log(a|b);

Output: 5 is represented as 101 and 3 is represented as 011. When the OR operation is performed 111 is returned which after conversion to decimal returns 7.

7

Example 2: In this example, we will use bitwise OR operation two combine bit values of both numbers.




let a=24;
let b=45;
console.log(a|b);

Output: 24 is represented as 11000 and 45 is represented as 101101. When the OR operation is performed 111101 is returned which contains all the bits of both numbers and returns 61 as decimal output.

61

Supported Browsers:

We have a complete list of JavaScript Bitwise Operators, to check those please go through, the JavaScript Bitwise Operators article

Article Tags :