3 ms·
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, i
by niggler 14y ago
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 http://es5.github.com/#x11.10 (or http://ecma-international.org/ecma-262/5.1/#sec-11.10 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/Ecma-262.pdf http://www.ecma-international.org/publications/files/ECMA-ST...
- evilpie 14y agoAs 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 http://ecma-international.org/ecma-262/5.1/#sec-11.10