javascriptfloating-pointinteger

Are there Integers in JavaScript?


MDN states:

Numbers in JavaScript are "double-precision 64-bit format IEEE 754 values", according to the spec. This has some interesting consequences. There's no such thing as an integer in JavaScript, so you have to be a little careful with your arithmetic if you're used to math in C or Java.

This suggests all numbers are floats. Is there any way to use integers, not float?


Solution

  • There are really only a few data types in JavaScript: Objects, numbers, and strings. As you read, JS numbers are all 64-bit floats. There are no ints.

    Firefox 4 will have support for Typed Arrays, where you can have arrays of real ints and such: https://developer.mozilla.org/en/JavaScript_typed_arrays

    Until then, there are hackish ways to store integer arrays as strings, and plucking out each integers using charCodeAt().