Python VS JavaScript – What are the Key Differences Between The ...
The main difference between these two approaches is that in Python, the user will be prompted to enter a value in the console while in ...
BigInt
A number literal like `37` in JavaScript code is a floating-point value, not an integer. There is no separate integer type in common everyday use. (JavaScript now has a <anchor>BigInt</anchor> type, but it was not designed to replace Number for everyday uses. `37` is still a Number, not a BigInt.)
BigInt - JavaScript | MDN
Search MDNClear search inputSearch

Theme

Article Actions

Filter sidebarClear filter input

## In this article

## In this article

# BigInt

## BaselineWidely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers sinceSeptember 2020.

`BigInt` values represent numeric values which are too large to be represented by the `number` primitive.

## Description

A BigInt value, also sometimes just called a BigInt, is a `bigint` primitive, created by appending `n` to the end of an integer literal, or by calling the `BigInt()` function (without the `new` operator) and giving it an integer value or string value.

js

BigInt values are similar to Number values in some ways, but also differ in a few key matters: A BigInt value cannot be used with methods in the built-in `Math` object and cannot be mixed with a Number value in operations; they must be coerced to the same type. Be careful coercing values back and forth, however, as the precision of a BigInt value may be lost when it is coerced to a Number value.

### Type information

When tested against `typeof`, a BigInt value ( `bigint` primitive) will give `&quot;bigint&quot;`:

js

A BigInt value can also be wrapped in an `Object`:

js

### Operators

Most operators support BigInts, however most do not permit operands to be of mixed types — both operands must be BigInt or neither:

The boolean-returning operators allow mixing numbers and BigInts as operands:

A couple of operators do not support BigInt at all:

Special cases:

- Addition ( `+`) involving a string and a BigInt returns a string.
- Division ( `/`) truncates fractional components towards zero, since BigInt is unable to represent fractional quantities.

js

### Comparisons

A BigInt value is not strictly equal to a Number value, but it is loosely so:

js

A Number value and a BigInt value may be compared as usual:

js

BigInt values and Number values may be mixed in arrays and sorted:

js

Note that comparisons with `Object`-wrapped BigInt values act as with other objects, only indicating equality when the same object instance is compared:

js

Because coercing between Number values and BigInt values can lead to loss of precision, the following are recommended:

- Only use a BigInt value when values greater than 253 are reasonably expected.
- Don't coerce between BigInt values and Number values.

### Conditionals

A BigInt value follows the same conversion rules as Numbers when:

- it is converted to a `Boolean`: via the `Boolean` function;
- when used with logical operators `||`, `&amp;&amp;`, and `!`; or
- within a conditional test like an `if` statement.

Namely, only `0n` is falsy; everything else is truthy.

js

### Cryptography

The operations supported on BigInt values are not constant-time and are thus open to timing attacks. JavaScript BigInts therefore could be dangerous for use in cryptography without mitigating factors. As a very generic example, an attacker could measure the time difference between `101n ** 65537n` and `17n ** 9999n`, and deduce the magnitude of secrets, such as private keys, based on the time elapsed. If you still have to use BigInts, take a look at the Timing attack FAQ for general advice regarding the issue.

### Use within JSON

Using `JSON.stringify()` with any BigInt value will raise a `TypeError`, as BigInt values aren't serialized in JSON by default. However, `JSON.stringify()` specifically leaves a backdoor for BigInt values: it would try to call the BigInt's `toJSON()` method. (It doesn't do so for any other primitive values.) Therefore, you can implement your own `toJSON()` method (which is one of the few cases where patching built-in objects is not explicitly discouraged):

js

Instead of throwing, `JSON.stringify()` now produces a string like this:

js

If you do not wish to patch `BigInt.prototype`, you can use the `replacer` parameter of `JSON.stringify` to serialize BigInt values:

js

You can then use the `reviver` parameter of `JSON.parse` to handle them:

js

Note: While it's possible to make the replacer of `JSON.stringify()` generic and properly serialize BigInt values for all objects as shown above, the reviver of `JSON.parse()` has to be used with caution, because the serialization is irreversible: it's not possible to distinguish between an object that happens to have a property called `$bigint` and an actual BigInt.

In addition, the example above creates an entire object during replacing and reviving, which may have performance or storage implications for larger objects containing many BigInts. If you know the shape of the payload, it may be better to just serialize them as strings and revive them based on the property key's name instead.

In fact, JSON allows number literals that are arbitrarily long; they just cannot be parsed to full precision in JavaScript. If you are communicating with another program in a language that supports longer integers (such as 64-bit integers), and you want to transmit the BigInt as a JSON number instead of a JSON string, see Lossless number serialization.

### BigInt coercion

Many built-in operations that expect BigInts first coerce their arguments to BigInts. The operation can be summarized as follows:

- BigInts are returned as-is.
- `undefined` and `null` throw a `TypeError`.
- `true` turns into `1n`; `false` turns into `0n`.
- Strings are converted by parsing them as if they contain an integer literal. Any parsing failure results in a `SyntaxError`. The syntax is a subset of string numeric literals, where decimal points or exponent indicators are not allowed.
- Numbers throw a `TypeError` to prevent unintended implicit coercion causing loss of precision.
- Symbols throw a `TypeError`.
- Objects are first converted to a primitive by calling their `[Symbol.toPrimitive]()` (with `&quot;number&quot;` as hint), `valueOf()`, and `toString()` methods, in that order. The resulting primitive is then converted to a BigInt.

The best way to achieve nearly the same effect in JavaScript is through the `BigInt()` function: `BigInt(x)` uses the same algorithm to convert `x`, except that Numbers don't throw a `TypeError`, but are converted to BigInts if they are integers.

## Constructor

Returns primitive values of type BigInt. Throws an error when called with `new`.

## Static methods

Clamps a BigInt value to a signed integer value, and returns that value.

Clamps a BigInt value to an unsigned integer value, and returns that value.

## Instance properties

These properties are defined on `BigInt.prototype` and shared by all `BigInt` instances.

The constructor function that created the instance object. For `BigInt` instances, the initial value is the `BigInt` constructor.

## Instance methods

Returns a string with a language-sensitive representation of this BigInt value. Overrides the `Object.prototype.toLocaleString()` method.

Returns a string representing this BigInt value in the specified radix (base). Overrides the `Object.prototype.toString()` method.

## Examples

### Calculating Primes

js

## Specifications

## Browser compatibility

BCD tables only load in the browser

## See also

## Help improve MDN

Was this page helpful to you?

YesNo

Your blueprint for a better internet.

## MDN

## Support

## Our communities

## Developers

Portions of this content are ©1998–2024 by individual mozilla.org contributors. Content available under a Creative Commons license.