The Number.isSafeInteger(number) method

JavaScript numbers are stored as 64-bit floating-point numbers, following the international IEEE 754 standard. This format stores numbers in 64 bits, where the number (the fraction) is stored in 0 to 51 bits, the exponent in 52 to 62 bits, and the sign in the last bit. So in JavaScript, safe integers are those numbers that do not need to be rounded to some other integer to fit in with the IEEE 754 representation. Mathematically, numbers from -(253-1) to (253-1) are considered as safe integers. Here is an example to demonstrate this:

console.log(Number.isSafeInteger(156));console.log(Number.isSafeInteger('1212'));console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER));console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.