Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The OR Operator (|) in Javascript does the same as parseInt? [duplicate]

Tags:

javascript

Possible Duplicate:
Using bitwise OR 0 to floor a number

Performs a bitwise OR on two expressions,eg:

console.log(12.22|0) // output --->12

where does the decimal go? it's the same as parseInt function

parseInt(12.22)  // output --->12

how does it work?

like image 659
Ryan Yiada Avatar asked May 11 '26 00:05

Ryan Yiada


1 Answers

parseInt is useful in cases where one is parsing strings such as "12px".

For example:

pasrseInt("12px"); // returns 12

However, this doesn't make any sense with bit-wise OR:

"12px" | 0; // returns 0

Performing a bit-wise OR is more like applying Math.floor() to a number -- bitwise operations work on 32-bit integers in Javascript.

like image 107
Peter Avatar answered May 12 '26 13:05

Peter



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!