Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there or isn't there an integer type in JavaScript?

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?

like image 753
Jay Souper Avatar asked Nov 18 '15 06:11

Jay Souper


People also ask

Is not an integer JavaScript?

The Number. isInteger() method in JavaScript is used to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise, it returns false.

How do you check whether a Number is integer or not in JavaScript?

The Number. isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .

Which of the following type does not exist in JavaScript?

In some languages, there is a special “character” type for a single character. For example, in the C language and in Java it is called “char”. In JavaScript, there is no such type. There's only one type: string .

What is the type of Number in JavaScript?

The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#. This means it can represent fractional values, but there are some limits to the stored number's magnitude and precision.


2 Answers

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.

like image 187
zerkms Avatar answered Sep 24 '22 00:09

zerkms


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" 
like image 37
Mathias Lykkegaard Lorenzen Avatar answered Sep 24 '22 00:09

Mathias Lykkegaard Lorenzen