JavaScript traps · JavaScript

JavaScript: 9999999999999999 prints 10000000000000000?!PRO

Numbers are 64-bit floats: above 2^53 not every whole number exists. BigInt (the n suffix) keeps every digit.

// 9999999999999999 prints 10000000000000000 ?!
// JavaScript numbers are 64-bit floats: above 2^53 (9007199254740992) not every whole number exists.
console.log(9999999999999999);          // 10000000000000000 ?!
console.log(Number.MAX_SAFE_INTEGER);   // 9007199254740991

// Fix: BigInt (the n at the end) keeps every digit
console.log(9999999999999999n);         // 9999999999999999n ✓
console.log(2n ** 64n);                 // 18446744073709551616n ✓
Open code JS12 in Pocket Compiler Download the Pocket Compiler app — free

No app yet? Install it, then open the app and type the code JS12 in Code Library.