4

Is "Number" in JavaScript entirely synonymous with "Integer"?

What piqued my curiosity:

--- PHP, Python, Java and others use the term "Integer" --- JavaScript has the function parseInt() rather than parseNumber()

Are there any details of interest?

mikej
  • 65,295
  • 17
  • 152
  • 131
m59
  • 43,214
  • 14
  • 119
  • 136
  • There are only floating point numbers in JavaScript, though integers can be represented as floating point numbers. – Qantas 94 Heavy Nov 28 '13 at 01:53
  • possible duplicate of [Integers in JavaScript](http://stackoverflow.com/questions/4703725/integers-in-javascript) – Qantas 94 Heavy Nov 28 '13 at 01:54
  • @Qantas94Heavy I see. I don't do very much math. So, should I delete this post? The other question isn't quite asking the same thing, but it is answered well. I googled for a while wondering what the difference was and couldn't find anything. – m59 Nov 28 '13 at 01:59
  • 1
    Your question is fine, closing as a duplicate in this case could be considered a way to link two questions plus their answers. Other visitors would benefit from the complementary information. – bfavaretto Nov 28 '13 at 02:25

2 Answers2

9

Is "Number" in JavaScript entirely synonymous with "Integer"?

No. All numbers in JavaScript are actually 64-bit floating point values.

parseInt() and parseFloat() both return this same data type - the only difference is whether or not any fractional part is truncated.

52 bits of the 64 are for the precision, so this gives you exact signed 53-bit integer values. Outside of this range integers are approximated.

In a bit more detail, all integers from -9007199254740992 to +9007199254740992 are represented exactly (-2^53 to +2^53). The smallest positive integer that JavaScript cannot represent exactly is 9007199254740993. Try pasting that number into a JavaScript console and it will round it down to 9007199254740992. 9007199254740994, 9007199254740996, 9007199254740998, etc. are all represented exactly but not the odd integers in between. The integers that can be represented exactly become more sparse the higher (or more negative) you go until you get to the largest value Number.MAX_VALUE == 1.7976931348623157e+308.

mikej
  • 65,295
  • 17
  • 152
  • 131
Matt
  • 20,108
  • 1
  • 57
  • 70
  • Nice, makes sense. I haven't done much math in any language, so I wouldn't have noticed. – m59 Nov 28 '13 at 02:00
3

In JavaScript there is a single number type: an IEEE 754 double precision floating point (what is called number.)

This article by D. Crockford is interesting:

http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/

Escualo
  • 40,844
  • 23
  • 87
  • 135