Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does |0 do in Javascript? [duplicate]

I have been looking into asm.js to use for a project recently, and I noticed that very often the asm.js compiled code will end a statement with |0;, even seemingly redundantly as in the statement i = i|0;

This is not something I have encountered in Javascript code before. What is it for?

EDIT

I don't believe this is duplicate. I know what a bitwise or is. I am specifically asking here why one might use it to or with a 0 before assignment. What purpose does that serve?

like image 572
Luke Avatar asked Dec 01 '25 15:12

Luke


1 Answers

Convert to integer and apply a bitwise or with 0. Basically a short form of Math.floor().

like image 148
Nina Scholz Avatar answered Dec 04 '25 07:12

Nina Scholz



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!