Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: what's difference between | and ||?

Tags:

javascript

I'm looking at some Javascript code that is:

if ( a>2 | b>4 ) { ... }

(ignore the ... above). What's the | doing? I assume it's logical OR, but all the references I could find online speak about ||, and I can't find anything mentioning just |. Thanks in advance

like image 212
ggkmath Avatar asked Dec 07 '25 06:12

ggkmath


1 Answers

It's the bitwise or. || is logical or.

The bitwise or (|) coerces the values to 32 bit integers and returns the 32 bit integer with each bit set to 1 if either of the two bits in the corresponding locations is 1 and 0 if they are both 0.

Logical or (||) evaluates to the first value if it's not falsey, otherwise it evaluates to the second value.

You almost definitely want || instead of |.

like image 164
aaronasterling Avatar answered Dec 09 '25 19:12

aaronasterling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!