In JavaScript there is no special type for integer values. Actually, all numbers has a Number
type which is Double Precision Floating Point Number (IEEE 754) i.e. binary64.
One of the problems with numeric operations in JavaScript is that there is no integer division operator or function defined in the standard.
UPDATE: actually, there is a Math.trunc()
function, but it works ~2-5 times slower than binary solution and it's not supported in Internet Explorer.
Some binary operators could be used to achieve the desired result, e.g.:
(10 / 4) >> 0
~~(10 / 4)
(10 / 4) | 0
All operations above produces number 2
and according to my benchmarks, have a similar performance on my x64
machine (Node.js v7.2.1
).
Could you please explain how exactly are those binary operations work for this exact purpose? Is there a preferred one?