64

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?

10
  • 20
    Yes there are integer values, but there is no integer Type, only Number. Implementation details, such as how they are stored, is not part of the language specification.
    – RobG
    Commented Nov 18, 2015 at 6: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
    Commented Nov 18, 2015 at 6: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
    Commented Nov 18, 2015 at 6:53
  • 1
    It gets extra special when you do bitwise operations on Numbers. Commented Nov 18, 2015 at 7:02
  • 1
    @AimanAl-Eryani They were a part of the abandoned ES4 draft. Commented Nov 18, 2015 at 7:24

5 Answers 5

62

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.

1
  • 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
    Commented Oct 10, 2018 at 12:28
25

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"
1
  • 4
    Can also be written var n = 1337n.
    – apostl3pol
    Commented Mar 4, 2021 at 17:20
8

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

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.

6
  • 1
    Does your last sentence refer to languages with integer types? Because 1/3 = 0.3333333333333333 in JavaScript. Commented Nov 18, 2015 at 6:54
  • 1
    Floating point numbers remain floated after becoming whole again, I believe, unless rounded with Math.floor() or .ceil() . Regardless, they are both Number types.
    – Phil C.
    Commented Nov 18, 2015 at 6:56
  • 1
    The // operator? That's a comment. Commented Nov 18, 2015 at 7:01
  • 1
    Ahaha.. yes -.-. That's with python. Thanks for pointing it out Commented Nov 18, 2015 at 7:05
  • 1
    "In Javascript, the type of a variable is implicitly defined" – No, in JavaScript, variables don't have types at all. Only values do. Commented Jun 26, 2021 at 7:09
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.