Parse integer strings
Converting user or configuration text such as "0x123" into an exact integer representation.
JavaScript · Function
BigInt(value) converts a supported value into a primitive BigInt. It accepts integer-like values, strings, booleans, and BigInts, but rejects fractional Numbers and cannot be called with new.
Call BigInt(value) without new to convert a supported value into a bigint primitive. Strings are parsed as integer-literal text, booleans become 1n or 0n, and Number inputs must be integral; fractional Numbers, invalid strings, and unsupported values throw.
BigInt(value)| Name | Type | Description |
|---|---|---|
value | string | number | boolean | bigint | The value to convert; it may be a string, an integer, a boolean, or another BigInt. |
A BigInt value.
const fromInteger = BigInt(9007199254740993);
const fromBinary = BigInt("0b10101");
console.log(fromInteger); // 9007199254740992n
console.log(fromBinary); // 21nConverts an integer Number and a binary-prefixed string into BigInt primitives.
Converting user or configuration text such as "0x123" into an exact integer representation.
Turning an integral Number into a BigInt when subsequent calculations require bigint values.
Mapping boolean flags to numeric BigInt values for bigint-based logic.
BigInt(value) returns a primitive whose type is bigint. It accepts values such as strings, integers, booleans, and existing BigInts, and it must be called without new.
Follow-up: What happens with BigInt(1.2)? It throws RangeError because Number input must be integral.
A decimal integer Number can be converted, but a non-integral Number throws RangeError. Conversion is explicit, so this is the documented route for converting an integer Number to BigInt.
Follow-up: Why can conversion alter a large Number? Number may already have lost precision before BigInt receives it; use a string or BigInt literal when exactness matters.
A string is interpreted using integer-literal syntax. Leading and trailing whitespace are allowed, and binary, octal, and hexadecimal prefixes can be used; an unparseable string throws SyntaxError.
Follow-up: What does BigInt("0x123") produce? It produces 291n, because the string is parsed as hexadecimal integer-literal text.
BigIntBigInt.asIntN()BigInt.asUintN()BigInt64ArrayBigUint64Array