How to Convert a JavaScript BigInt Value to a String?

You can convert a bigint to a string by using the String wrapper object or the BigInt.prototype.toString() method like so:

// ES10+
const MAX_SAFE_INTEGER = 9007199254740991n;

console.log(String(MAX_SAFE_INTEGER)); // '9007199254740991'
console.log(MAX_SAFE_INTEGER.toString()); // '9007199254740991'

You may also use the BigInt.prototype.toLocaleString() method if you wish to output the bigint value as a formatted string based on a specific locale. For example:

console.log(MAX_SAFE_INTEGER.toLocaleString('de-DE'));
// '9.007.199.254.740.991'

console.log(MAX_SAFE_INTEGER.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
// '9.007.199.254.740.991 €'

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.