Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active December 23, 2020 16:38
Show Gist options
  • Save rpivo/6b1356595166c094c5d5e2bb87269cd5 to your computer and use it in GitHub Desktop.
Save rpivo/6b1356595166c094c5d5e2bb87269cd5 to your computer and use it in GitHub Desktop.
BigInt in JavaScript

BigInt in JavaScript

BigInt is a relatively new JavaScript data type introduced in 2020.

JavaScript's original Number primitive can only represent numbers up to 253 - 1.

We can log this maximum value with Number.MAX_SAFE_INTEGER:

console.log(Number.MAX_SAFE_INTEGER)
// 9007199254740991

BigInts can be bigger than this number. We can construct a BigInt using the n suffix:

const bigFella = 10000000000000000000n

console.log(bigFella)
// 10000000000000000000n

We can also construct a BigInt with the BigInt() constructor:

const anotherBigFella = BigInt(10000000000000000000)

console.log(anotherBigFella)
// 10000000000000000000n

Logging the BigInt won't magically turn it into a Number, and at no point will the BigInt be transformed into a Number. It's a different data type, and it will always retain the n suffix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment