let myNumber = 9007199254740992; myNumber++; console.log('myNumber', myNumber); //9007199254740992
We would have to get access to this numeric constant through Number.MAX_SAFE_INTEGER. For instance:
const max = Number.MAX_SAFE_INTEGER; console.log(max); //9007199254740991
One less than 9007199254740992—since with 9007199254740991 we can still perform all of the normal actions including increasing.
let myNumber = BigInt(9007199254740992); myNumber++; console.log('myNumber', myNumber.toString()); //9007199254740993
Alternatively, attaching the letter ‘n’ to the number automatically turns it into a BigInt:
let myNumber = 9007199254740992n; myNumber++; console.log('myNumber', myNumber.toString()); //9007199254740993 console.log(typeof myNumber); //bigint
It’s important to note that we’re talking about a primitive data type that’s completely different. If you try to compare 42 to 42n, it will return false. This is just like if you tried to compare a text string to a number. And if you want to make a comparison? Well, you’ll have to convert the type—or the number to BigInt, or the BigInt to a number …
console.log(42 == 42n); //true console.log(42 === 42n); //false console.log(42 === Number(42n)); //true console.log(BigInt(42) === 42n); //true
With BigInt, you can perform mathematical operations or whatever you want. If you want to add a text string or a number, first you’ve got to convert it. Also note, decimals aren’t supported with BigInt—if there are any they’ll be rounded up.
const rounded = 5n / 2n; console.log('rounded', rounded.toString()); //2
About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen.