BigInt.prototype.toString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.

The toString() method of BigInt values returns a string representing the specified BigInt 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

js
toString()
toString(radix)

Parameters

radix Optional

An 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 specified BigInt value.

Exceptions

RangeError

Thrown if radix is less than 2 or greater than 36.

Description

The BigInt object overrides the toString method of Object; it does not inherit Object.prototype.toString(). For BigInt values, the toString() 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 through f 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.

The toString() method requires its this value to be a BigInt primitive or wrapper object. It throws a TypeError for other this values without attempting to coerce them to BigInt values.

Because BigInt doesn't have a [Symbol.toPrimitive]() method, JavaScript calls the toString() method automatically when a BigInt object is used in a context expecting a string, such as in a template literal. However, BigInt primitive values do not consult the toString() method to be coerced to strings — rather, they are directly converted using the same algorithm as the initial toString() implementation.

js
BigInt.prototype.toString = () => "Overridden";
console.log(`${1n}`); // "1"
console.log(`${Object(1n)}`); // "Overridden"

Examples

Using toString()

js
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-zero BigInt as there are no negative zeros in integers. -0.0 is an IEEE floating-point concept that only appears in the JavaScript Number type.

js
(-0n).toString(); // "0"
BigInt(-0).toString(); // "0"

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-bigint.prototype.tostring

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
toString
Chrome – Full support
Chrome 67 (Release date: 2018-05-29)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 68 (Release date: 2019-07-09)
footnote Full support
Opera – Full support
Opera 54 (Release date: 2018-06-28)
footnote Full support
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote Full support
Chrome Android – Full support
Chrome Android 67 (Release date: 2018-05-31)
footnote Full support
Firefox for Android – Full support
Firefox for Android 68 (Release date: 2019-07-09)
footnote Full support
Opera Android – Full support
Opera Android 48 (Release date: 2018-11-08)
footnote Full support
Safari on iOS – Full support
Safari on iOS 14 (Release date: 2020-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 9 (Release date: 2018-09-15)
footnote Full support
WebView Android – Full support
WebView Android 67 (Release date: 2018-05-31)
footnote Full support
WebView on iOS – Full support
WebView on iOS 14 (Release date: 2020-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 10.4 (Release date: 2018-06-06)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also