JavaScript BigInt Primitive
A bigint primitive represents an integer value of arbitrary size.
The type name is bigint.
BigInt values are one of JavaScript's 7 primitive types.
Why BigInt Exists
JavaScript numbers cannot safely represent all integers.
The largest safe integer is:
9007199254740991
BigInt values can represent much larger integers.
Creating BigInt Values
A bigint value can be created by appending n to an integer literal.
Type Information
The type of a primitive bigint value is "bigint".
Using BigInt()
The BigInt() method converts values to bigint values.
| Expression | Result |
|---|---|
BigInt(123) |
123n |
BigInt("456") |
456n |
BigInt Arithmetic
BigInt values support arithmetic operations.
| Operator | Example |
|---|---|
| + | 10n + 5n |
| - | 10n - 5n |
| * | 10n * 5n |
| / | 10n / 5n |
| ** | 10n ** 2n |
BigInt Division
BigInt division truncates fractional results.
BigInt and Number
BigInt and Number are different primitive types.
They cannot be mixed directly in arithmetic expressions.
Example
10n + 5; // Throws a TypeError
Do not mix bigint and number values in arithmetic operations.
Comparing BigInt and Number
BigInt and Number values can be compared.
Example
10n == 10; // true
No Constructor
BigInt is not a constructor.
The new keyword cannot be used with BigInt.
Example
new BigInt(123); // Throws a TypeError
BigInt vs Number
| BigInt | Number |
|---|---|
| Arbitrary-size integers | 64-bit floating-point values |
| No decimal values | Supports decimals |
| Primitive type: bigint | Primitive type: number |