63

I am just starting to learn Javascript and I immediately got confused by seemingly contradictory statements in Mozilla's A re-introduction to JavaScript (JS tutorial).

One one hand:

"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."

On the other hand (immediately after that paragraph):

"In practice, integer values are treated as 32-bit ints (and are stored that way in some browser implementations), which can be important for bit-wise operations."

and

"You can convert a string to an integer using the built-in parseInt() function."

So, is there such thing as integer in JavaScript or isn't there?

Anders
  • 8,307
  • 9
  • 56
  • 88
Jay Souper
  • 2,576
  • 2
  • 14
  • 17
  • 19
    Yes there are integer values, but there is no integer [*Type*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-ecmascript-data-types-and-values), only [*Number*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-ecmascript-language-types-number-type). Implementation details, such as how they are stored, is not part of the language specification. – RobG Nov 18 '15 at 06:39
  • 2
    @RobG Your concise answer is full and completely cleared my confusion. Please post it as an answer so that I can accept. Thank you very much. – Jay Souper Nov 18 '15 at 06:48
  • 1
    @RobG it's a part of specification for sure: "primitive value corresponding to a double-precision 64-bit binary format IEEE 754-2008 value" – zerkms Nov 18 '15 at 06:53
  • 1
    It gets extra special when you do bitwise operations on Numbers. – David Ehrmann Nov 18 '15 at 07:02
  • @RobG please put your comment as an answer (with a tiny correction about IEEE754), since the currently provided answers are awful. Thanks :-) – zerkms Nov 18 '15 at 07:08
  • @zerkms: You wrote down in one of the (apparently deleted) answers that the types like int aren't precisely defined to Javascript. I wonder what the `int`, `boolean` ..etc keywords are reserved for then. And what about `typeof`? – Aiman Al-Eryani Nov 18 '15 at 07:17
  • @AimanAl-Eryani they are not reserved http://www.ecma-international.org/ecma-262/6.0/index.html#sec-keywords – zerkms Nov 18 '15 at 07:18
  • @zerkms & AimanAl-Eryani They used to be reserved up through ES3, but they are not anymore. – Alexander O'Mara Nov 18 '15 at 07:19
  • 1
    @AimanAl-Eryani They were a part of [the abandoned ES4 draft](http://www.ecmascript.org/es4/spec/overview.pdf). – Alexander O'Mara Nov 18 '15 at 07:24
  • In https://github.com/mdn/content/commit/187777c I updated the MDN *“A re-introduction to JavaScript (JS tutorial)”* article to instead say, *“where this article and other MDN articles refer to "integers", what’s usually meant is a **representation** of an integer using a Number value”* — and also to mention BigInt. If anybody has suggestions for further improvement there, please raise an issue at https://github.com/mdn/content/issues/ (or else a pull request, in the same repo). – sideshowbarker Mar 24 '21 at 03:33

5 Answers5

58

UPDATE: with a new ES2020 standard released this answer is not entirely correct anymore, see the other answer (from @Mathias Lykkegaard Lorenzen) about BigInt details.

There is only the Number data type in JS that represents numbers.

Internally it is implemented as IEEE 754 double precision floating point number.

What it means is that - technically there is no dedicated data type that represents integer numbers.

Practically it means that we can safely use only numbers that are safely representable by the aforementioned standard. And it includes integer values in the range: [-9007199254740991; 9007199254740991]. Both values are defined as constants: Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER correspondingly.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • Besides the range of numbers everyone should also think about floating point's special values like infinity (no divide by zero in JS), -infinity, NaN and even -0 (which is not the same as zero!). – J D Oct 10 '18 at 12:28
20

I should mention that there is actually a type called BigInt which represents a true integer.

However, because it can't be used with Number and is generally only a good idea to use for larger numbers, I wouldn't advise it.

I thought it was worth a mention though.

var n = BigInt(1337);
console.log(typeof n); //prints "bigint"
Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
7

I believe questioner has already found their answer. But for others like me, you can check number is integer or not in JavaScript by using Number.isInteger() method. MDN

Community
  • 1
  • 1
sabbir
  • 2,020
  • 3
  • 26
  • 48
0

Ah yes, this can be quite confusing. In Javascript, the type of a variable is implicitly defined.

the function parseInt will simply not take the decimal point into account and the type of the result would be an int.

Aiman Al-Eryani
  • 709
  • 4
  • 19
0

Bitwise operators use a "hidden" 32 bit integer, but it can't be used directly in the language, it simply "appears"

Cesc
  • 21
  • 1