1

Recently, during a discussion with one of my colleagues, I heard about this new data type in JavaScript - BigInt.

Things I know so far: In JavaScript, there is a limitation while using the Number type; it cannot safely represent integer values larger than 2 to the power of 53. This limitation has often forced developers to use inefficient workarounds and third-party libraries to represent much larger numbers. BigInt is a new data type intended to fix that.

Things I want to know: (A single question broken into two points for better clarity)

  • In JavaScript, how exactly BigInt is implemented / defined?
    • Is it a new data type (similar to Number, Boolean, etc)?
    • Or implemented similar to existing JavaScript constants like MAX_SAFE_INTEGER, Infinity, -Infinity etc?

Apart from the above question, something additional to think about: (Adding here for reference only): Sometime back, I wrote this SO post about Can a number in JavaScript ever reach to Infinity in runtime?. Is that particular scenario in JavaSscript going to get changed / affected by the introduction of BigInt?

Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36
  • 3
    looking on [`mdn`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) `BigInt` is a new primitive type – jonatjano Aug 20 '19 at 09:43
  • @jonatjano then same would be the case with all the popular questions on SO with hundreds of upvotes. Google provides a good list of result for all of them. I posted here for the benefit of SO community. – Aaditya Sharma Aug 20 '19 at 10:02
  • Important thing I learnt from @certainperformance's answer - ***A `BigInt` is not a `Number` - it is a `BigInt`.*** – Aaditya Sharma Aug 20 '19 at 10:10

1 Answers1

3

BigInt is a finished proposal, at stage 4.

As MDN says:

BigInt will become the second built-in numeric type in JavaScript.

BigInt is slated to become the first new primitive type added to JavaScript since Symbol in ES2015.

You can see many of the implementation examples on the proposal page, and for the nitty gritty details, you can read the proposal specification here.

Or implemented similar to existing JavaScript constants like MAX_SAFE_INTEGER, Infinity, -Infinity etc?

A BigInt is not a number - it is a BigInt. (for the existing constants we're familiar with, typeof Infinity and typeof MAX_SAFE_INTEGER and so on, all give number in return)

Is that particular scenario in JavaSscript going to get changed / affected by the introduction of BigInt?

I think that when a number gets too large for a BigInt to handle, it will throw a RangeError (instead of evaluating to a BigInt version of Infinity), at least in V8:

3n ** 3n ** 3n ** 3n

Uncaught RangeError: Maximum BigInt size exceeded

See discussion:

V8 currently allows 1 billion bits for a BigInt. The limit was 1 million bits for a while, but we found tests that ran into that limit, so we bumped it. We may raise or lower it again if reasons emerge to do so. (Personally, I think it is reasonable to assume that "every implementation will allow at least 1 million bits".)

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320