BigInt.prototype.toString()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.
ThetoString()
method ofBigInt
values returns a string representing the specifiedBigInt
value. The trailing "n" is not part of the string.
Try it
console.log(1024n.toString());// Expected output: "1024"console.log(1024n.toString(2));// Expected output: "10000000000"console.log(1024n.toString(16));// Expected output: "400"
Syntax
toString()toString(radix)
Parameters
radix
OptionalAn integer in the range 2 through 36 specifying the base to use for representing the BigInt value. Defaults to 10.
Return value
A string representing the specifiedBigInt
value.
Exceptions
RangeError
Thrown if
radix
is less than 2 or greater than 36.
Description
TheBigInt
object overrides thetoString
method ofObject
; it does not inheritObject.prototype.toString()
. ForBigInt
values, thetoString()
method returns a string representation of the value in the specified radix.
For radixes above 10, the letters of the alphabet indicate digits greater than 9. For example, for hexadecimal numbers (base 16)a
throughf
are used.
If the specified BigInt value is negative, the sign is preserved. This is the case even if the radix is 2; the string returned is the positive binary representation of the BigInt value preceded by a-
sign,not the two's complement of the BigInt value.
ThetoString()
method requires itsthis
value to be aBigInt
primitive or wrapper object. It throws aTypeError
for otherthis
values without attempting to coerce them to BigInt values.
BecauseBigInt
doesn't have a[Symbol.toPrimitive]()
method, JavaScript calls thetoString()
method automatically when aBigInt
object is used in a context expecting a string, such as in atemplate literal. However, BigIntprimitive values do not consult thetoString()
method to becoerced to strings — rather, they are directly converted using the same algorithm as the initialtoString()
implementation.
BigInt.prototype.toString = () => "Overridden";console.log(`${1n}`); // "1"console.log(`${Object(1n)}`); // "Overridden"
Examples
Using toString()
17n.toString(); // "17"66n.toString(2); // "1000010"254n.toString(16); // "fe"(-10n).toString(2); // "-1010"(-0xffn).toString(2); // "-11111111"
Negative-zero BigInt
There is no negative-zeroBigInt
as there are no negative zeros in integers.-0.0
is an IEEE floating-point concept that only appears in the JavaScriptNumber
type.
(-0n).toString(); // "0"BigInt(-0).toString(); // "0"
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-bigint.prototype.tostring |