The Number.isSafeInteger()
method determines whether the provided value is a number that is a safe integer. A safe integer is an integer that
For example, 253 - 1
is a safe integer: it can be exactly represented, and no other integer rounds to it under any IEEE-754 rounding mode. In contrast, 253
is not a safe integer: it can be exactly represented in IEEE-754, but the integer 253 + 1
can't be directly represented in IEEE-754 but instead rounds to 253
under round-to-nearest and round-to-zero rounding.
The safe integers consist of all integers from -(253 - 1)
inclusive to 253 - 1
inclusive.
Number.isSafeInteger(testValue)
testValue
A Boolean
indicating whether or not the given value is a number that is a safe integer.
Number.isSafeInteger(3); // true Number.isSafeInteger(Math.pow(2, 53)); // false Number.isSafeInteger(Math.pow(2, 53) - 1); // true Number.isSafeInteger(NaN); // false Number.isSafeInteger(Infinity); // false Number.isSafeInteger('3'); // false Number.isSafeInteger(3.1); // false Number.isSafeInteger(3.0); // true
Number.isSafeInteger = Number.isSafeInteger || function (value) { return Number.isInteger(value) && Math.abs(value) <= Number.MAX_SAFE_INTEGER; };
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Number.isSafeInteger' in that specification. | Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Number.isSafeInteger' in that specification. | Draft |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | 32 (32) | No support | (Yes) | 10.0 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | No support | No support | 32.0 (32) | No support | No support | No support |
Number
object it belongs to.Number.MIN_SAFE_INTEGER
Number.MAX_SAFE_INTEGER
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger