Zorak
Zorak

Reputation: 709

javascript/jQuery VAR maxium length

I tried to google it, but all key words references to funtions or solutions working with content of variable.

My question is simple. If variable represents a number,

var a = 1;

what is its max bit length? I mean, what highest number can it contain before buffer overflow happens? Is it int32? Is it int64? Is it a different length?

Thanks in advance

Upvotes: 0

Views: 106

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172438

Numbers in Javascript are IEEE 754 floating point double-precision values which has a 53-bit mantissa. See the MDN:

Integer range for Number

The following example shows minimum and maximum integer values that can be represented as Number object (for details, refer to ECMAScript standard, chapter 8.5 The Number Type):

var biggestInt = 9007199254740992;
var smallestInt = -9007199254740992;

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1074276

As the spec says, numbers in JavaScript are IEEE-754 double-precision floating point:

  • They're 64 bits in size.
  • Their range is -1.7976931348623157e+308 through 1.7976931348623157e+308 (that latter is available via Number.MAX_VALUE), which is to say the positive and negative versions of (2 - 2^-52) × 2^1023, but they can't perfectly represent all of those values. Famously, 0.1 + 0.2 comes out as 0.30000000000000004; see Is floating-point math broken?
  • The max "safe" integer value (whole number value that won't be imprecise) is 9,007,199,254,740,991, which is available as Number.MAX_SAFE_INTEGER on ES2015-compliant JavaScript engines.
  • Similarly, MIN_SAFE_INTEGER is -9,007,199,254,740,991

Upvotes: 4

Related Questions