What's the difference between an integer number and a safe integer number? and why is there this difference?

https://www.codecademy.com/courses/introduction-to-javascript/lessons/introduction-to-javascript/exercises/libraries

I was reading the JavaScript documentation to find a method on the built-in Number object that checks if a number is an integer and i reand about the “Number.isInteger()” and the “Number.isSafeInteger()” methods and i asked myself why are some number safe and other not,
can anyone help me?
Regards
Marco

Hello, and welcome to the forums!

The technical reason behind it is the following that was in a different part of the documentation:

The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(253 - 1) and 253 - 1

What it basically means it that JavaScript’s integer can’t handle less than -9007199254740991 or greater than 9007199254740991. (That’s - / + about 9 quadrillion)

However, if you do need to handle values less than or greater than that, then JavaScript now has a BigInt that you can read about here. Keep in mind that this is relatively recent addition and isn’t supported everywhere, so be sure to check the compatibility chart. For example, node didn’t support it until version v10.4.0, but Codecademy’s node environment for the JavaScript lessons is v7.10.1, so you can’t experiment with it there.

2 Likes