3

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?

7
  • just see doc: developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Oct 27, 2015 at 11:52
  • 1
    @Grundy I know what a bitwise or is. I just didn't understand the purpose of it in this case. Commented Oct 27, 2015 at 11:53
  • so, see duplicated question Commented Oct 27, 2015 at 11:54
  • from doc: Bitwise ORing any number x with 0 yields x. Bitwise ORing any number x with -1 yields -1. Commented Oct 27, 2015 at 11:55
  • You can write it even shorter: i |= 0 Commented Oct 27, 2015 at 11:55

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

Note that undefined NaN and null are also turned into 0 with this! :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.