JavaScript BigInt
BigInt is a JavaScript data type for handling and storing big integer values.
What is JavaScript BigInt?
BigInt is a JavaScript data type for handling and storing big integer values.
BigInt allows you to work with integers larger than the limit of Numbers.
BigInt can represent an integer of any size, limited only by available memory.
JavaScript Accuracy
JavaScript Numbers are only accurate up to 15 digits:
Example
Example Code
javascript// 15 digits:
let x = 999999999999999;
// 16 digits:
let y = 9999999999999999;Numbers are 64-bits Floating Point
All JavaScript Numbers are stored in a 64-bit floating-point format (IEEE 754 standard).
With this standard, large numbers cannot be exactly represented, but will be rounded.
JavaScript can only safely represent integers up to 253-1 (9007199254740991).
JavaScript can only safely represent integers down to -253-1 (-9007199254740991).
Examples
Example Code
javascript// MAX = 9007199254740991
let x = Number.MAX_SAFE_INTEGER;
// MIN = -9007199254740991
let y = Number.MIN_SAFE_INTEGER;How to Create a BigInt
You can create a BigInt in two ways:
- Using an integer literal with an n suffix
- Using the BigInt() constructor with a string
Examples
Example Code
javascript// Using an integer literal with an n suffix:
let x = 999999999999999n;
// Using the BigInt() constructor with a string:
let y = BigInt("999999999999999");You can also create a BigInt using the Bigint() constructor with a Number.
Warning !! Numbers are only accurate up to 15 digits.
Examples
Example Code
javascriptlet x = BigInt(9999999999999999);BigInt is a JavaScript Datatype
The JavaScript typeof a BigInt is "bigint":
Example
Example Code
javascriptlet x = BigInt(999999999999999);
let type = typeof x;BigInt is the second numeric data type in JavaScript (after Number).
With BigInt the total number of supported data types in JavaScript is 8:
1. String 2. Number 3. Bigint 4. Boolean 5. Undefined 6. Null 7. Symbol 8. Object
Arithmetic Operators
BigInt supports standard JavaScript arithmetic operators.
(+, -, ++, --, , /, %, )*
Example
Example Code
javascriptlet x = 9007199254740995n;
let y = 9007199254740995n;
let z = x * y;Mixing BigInt and Numbers
Arithmetic between a BigInt and a Number is not allowed (will result in a TypeError).
Explicit conversion must be done first.
Example
Example Code
javascriptlet x = 10n;
let y = 5;
let z = x + y; // ❌ TypeErrorBigInt / Number Conversions
BigInt to Number: Use the Number() constructor.
Number to BigInt: Use the BigInt() constructor.
Example
Example Code
javascript// Create a BigInt
let largeNumber = BigInt("12345678901234567890");
// Conversions
let num = Number(largeNumber);BigInt Decimals
A BigInt can not have decimals.
Example
Example Code
javascriptlet x = 1.5n; // ❌ TypeErrorBigInt Division Example
Example Code
javascriptlet x = 5n;
let y = x / 2;
// ❌ Error: Cannot mix BigInt and other types, use explicit conversion.Comparison Operators
Bigint supports standard JavaScript comparison operators.
( ==, ===, !==, =)
BigInts can also be compared with Numbers using standard comparison operators.
Example
Example Code
javascript// true
let x = (10n > 5n);
// false (different types)
let y = (10n === 10);
// true (loose equality)
let z = (10n == 10);Bitwise Operators
BigInt supports bitwise operations, but only with other BigInts (not Numbers):
- & (AND)
- | (OR)
- ^ (XOR)
- ~ (NOT)
Example
Example Code
javascriptlet a = 5n; // 0101
let b = 3n; // 0011
let x = (a & b); // 1n (0001)
let y = (a | b); // 7n (0111)
let z = (a ^ b); // 6n (0110)
let n = (~a); // -6nBitwise Shift Operators
BigInt only supports two shift operators:
- > (signed right shift)
Example
Example Code
javascriptlet big = 10n; // binary: 1010
let x = (big > 1n); // 5n (0101)Impotant Rules:
- Both operands must be BigInt
- Shift amounts must be non-negative
- Right shift keeps the sign bit for negative values
BigInt Hex, Octal and Binary
BigInt can also be written in hexadecimal, octal, or binary notation:
Like numbers, bigint literals support several bases:
- Normal: 256n
- Octal: 0o400n
- Hexadecimal: 0x100n
- Binary: 0b100000000n
Examples
Example Code
javascriptlet num = 256n;
let oct = 0o400n;
let hex = 0x100n;
let bin = 0b100000000n;Precision Curiosity
Maximum safe integer in JavaScript is 9007199254740991.
Rounding can compromise program security:
MAX_SAFE_INTEGER Examples
Example Code
javascript9007199254740992 === 9007199254740993; // is true !!!Browser Support
BigInt() is an ES2020 feature.
ES2020 is fully 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 |
Interactive Code Examples
JS Example 1
javascript// 15 digits:
let x = 999999999999999;
// 16 digits:
let y = 9999999999999999;Examples
javascript// MAX = 9007199254740991
let x = Number.MAX_SAFE_INTEGER;
// MIN = -9007199254740991
let y = Number.MIN_SAFE_INTEGER;Examples
javascript// Using an integer literal with an n suffix:
let x = 999999999999999n;
// Using the BigInt() constructor with a string:
let y = BigInt("999999999999999");Examples
javascriptlet x = BigInt(9999999999999999);JS Example 5
javascriptlet x = BigInt(999999999999999);
let type = typeof x;JS Example 6
javascriptlet x = 9007199254740995n;
let y = 9007199254740995n;
let z = x * y;