Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JavaScript use the term "Number" as opposed to "Integer"?

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?

like image 443
m59 Avatar asked Dec 16 '22 03:12

m59


2 Answers

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.

like image 187
Matt Avatar answered Dec 17 '22 17:12

Matt


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/

like image 36
Escualo Avatar answered Dec 17 '22 15:12

Escualo