Hacker News new | comments | show | ask | jobs | submit login

x|0 is a standard idiom, as explained in the ECMAScript Spec:

    The production A : A @ B, where @ is one of the bitwise operators in the productions above, is evaluated as follows:

    - Let lref be the result of evaluating A.
    - Let lval be GetValue(lref).
    - Let rref be the result of evaluating B.
    - Let rval be GetValue(rref).
    - Let lnum be ToInt32(lval).* <-- Convert to 32 bit Integer
    - Let rnum be ToInt32(rval).*
    - Return the result of applying the bitwise operator @ to lnum and rnum. The result is a signed 32 bit integer.
x|0 is therefore the result of ToInt32(x), which is effectively a coercion to integer.

Directed link to the section in the annotated spec: http://es5.github.com/#x11.10 (or http://ecma-international.org/ecma-262/5.1/#sec-11.10 -- as evilpie pointed out in a reply)

For the original spec, read Pages 82-83 of http://www.ecma-international.org/publications/files/ECMA-ST...

As of a few months ECMA now hosts their own html version of the spec. http://ecma-international.org/ecma-262/5.1/#sec-11.10


Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact