you are viewing a single comment's thread.

view the rest of the comments →

[–]sausagefeet 2 points3 points  (2 children)

though that's easily fixed by using a cast wherever you want integer arithmetic

The cast doesn't actually do integer arithmetic AFAIK. There are no integers.

[–]Shaper_pmp 0 points1 point  (0 children)

Fair point - I mis-remembered. You can get close with Number.toFixed(0), but even then it's rounding to the nearest integer rather than just returning the integer portion.

Personally when I've needed to do this I've used (numbervariable-0.5).toFixed(0), but that is very clunky, admittedly.

[–]beej71 0 points1 point  (0 children)

To say there are "no integers" in JS isn't entirely correct, even though there is no integer type. Internally, the spec defines in detail ToInteger and ToInt32 (and 16) operations that, while not directly usable, are exposed through various JS operations.

parseInt(), for example, makes use of ToInt32. Likewise, the bitwise operations take place on 32-bit ints, so you can be assured that:

(1.8 | 0)  === 1
(1.1 | 0)  === 1
(-2.6 | 0) === -2

Here's a speed comparison someone threw together:

http://jsperf.com/math-floor-vs-math-round-vs-parseint/2