15

I just came across JavaScript's new data type BigInt. What are some of the real-time use cases where we have to use BigInt vs Number?

I also see that it doesn't have cross-browser compatibility at this point.

4
  • 1
    Refer to MDN docs: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 3, 2020 at 23:58
  • 3
    any time you need a number bigger than 2<sup>53</sup> - 1 Commented May 4, 2020 at 0:03
  • Best use-case will be using it for handling large numbers like times... Commented May 4, 2020 at 0:06
  • 3
    Why is the question closed? Shouldn't we make it easy for someone to refer to the difference between them directly here without reading whole doc? Commented May 4, 2020 at 0:14

3 Answers 3

18

MDN doc

BigInt is a built-in object that provides a way to represent whole numbers larger than 2^53 - 1, which is the largest number JavaScript can reliably represent with the Number primitive and represented by the Number.MAX_SAFE_INTEGER constant. BigInt can be used for arbitrarily large integers.

Difference:

  • BigInt cannot be used with methods in the built-in Math object and cannot be mixed with instances of Number in operations
  • Because coercing between Number and BigInt can lead to loss of precision, it is recommended to only use BigInt when values greater than 2^53 are reasonably expected and not to coerce between the two types.
Sign up to request clarification or add additional context in comments.

1 Comment

side note for typescript users: typescriptlang.org/docs/handbook/2/… "JavaScript does not have a special runtime value for integers, so there’s no equivalent to int or float - everything is simply number"
7

The differences between BigInt and Number:

Number BigInt
Safe Range Number.MAX_SAFE_INTEGER; loses precision outside of this range Extremely large integers
Math operations Supports the Math object Basic arithmetic operations: + * - % **
Decimal vs. Integer support Integers and decimals, e.g 1.00, 2.56 Only integers, e.g 1n, 2n. 5n / 2 === 2n
Syntax No special syntax Append n to the end of a number literal or use new BigInt('100')
Type Conversion Automatically converts Number to BigInt in operations Requires explicit conversion to Number for arithmetic involving Number values
JSON Supported by default Not serializable by default

Use BigInt when you need to work with extremely large integers or require precise integer arithmetic without any loss of precision.

Comments

3

The Number object uses a 64-bit floating-point format. With 64 bits allocated in memory, you have a sign bit, 11 bits for exponent, and 52 bits for the mantissa. Since you only have 52 bits to represent the mantissa, you can only safely represent integers between -(2^53 - 1) and 2^53 - 1. So what does this mean for Number and how does BigInt address it? Examples are usually helpful:

const nval1 = 9007199254740991 + 1
const nval2 = 9007199254740991 + 2
nval1 === nval2
# => true

Clearly, by using the comparison operator with numbers that extend beyond Number.MAX_SAFE_INTEGER leads to some unexpected results. Now let's try it with BigInt:

const bval1 =  9007199254740991n + 1n
const bval2 =  9007199254740991n + 2n
bval1 === bval2
# => false

In terms of practical use, BigInt was added to ES2020 to allow the representation of 64-bit integers, which are required for compatibility with many other programming languages and APIs.

2 Comments

"BigInt was added to ES2020 to allow the representation of 64-bit integers" I wouldn't say that was the only or main goal, since at least according to the spec it's for arbitrary size.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.