JavaScript BigInt()
The BigInt() function creates BigInt values.
BigInt values can represent integers larger than JavaScript's safe integer range.
BigInt is a primitive data type.
Syntax
BigInt(value)
Parameters
| Parameter | Description |
|---|---|
| value | A value to convert to a bigint. |
Return Value
| Call | Returns |
|---|---|
BigInt(value) |
A primitive bigint value |
Creating a BigInt
Example
Literal Syntax
const x = 123n;
Example
Function Syntax
const x = BigInt(123);
BigInt Type
Example
const x = 123n;
(typeof x) // bigint
Large Integers
BigInt values can safely represent very large integers.
Example
const x = 999999999999999999999999999999999999n;
BigInt vs Number
| BigInt | Number |
|---|---|
| Arbitrary-size integers | 64-bit floating-point values |
| Primitive type: bigint | Primitive type: number |
| No decimal values | Supports decimals |
Conversion
| Expression | Result |
|---|---|
BigInt(123) |
123n |
BigInt("123") |
123n |
Important Warning
BigInt values cannot be mixed directly with Number values.
Example
const x = 10n;
const y = 5;
x + y; // Error
No Constructor
BigInt is not a constructor.
The new keyword cannot be used with BigInt.
Example
new BigInt(123); // TypeError
| What | Value |
|---|---|
| Callable | Yes |
| Constructible | No |
| Returns | Primitive bigint values |
| Primitive Type | bigint |
Primitives Compared
| Function | Constructible |
|---|---|
| String() | Yes |
| Number() | Yes |
| Boolean() | Yes |
| BigInt() | No |
| Symbol() | No |
Browser Support
BigInt() is a JavaScript 2020 feature.
ES 2020 is supported in all modern browsers since September 2020:
| Chrome 85 |
Edge 85 |
Firefox 79 |
Safari 14 |
Opera 71 |
| Aug 2020 | Aug 2020 | Mar 2020 | Sep 2020 | Sep 2020 |